单链表和双链表

来源:互联网 发布:mysql slave 编辑:程序博客网 时间:2024/05/16 11:14


1、单链表

一、定义自己的单链表:

public interface MyList {// 获取链表的长度public int size();// 添加元素public void add(String value);// 得到指定位置的元素public Node get(int index);// 在指定位置添加元素public void add(String value, int index);// 移除指定位置的元素public void remove(int index);}

二、定义节点:

public class Node {  public String date ;public Node next;/** * 构造方法得到数据和下一个节点 * @param date * @param next */public Node(String date, Node next) {super();this.date = date;this.next = next;}/** * 得到数据和下一个节点 * @return */public String getDate() {return date;}public void setDate(String date) {this.date = date;}public Node getNext() {return next;}public void setNext(Node next) {this.next = next;}}


三、实现自己的单链表

public class MyLinkedList implements MyList {public Node head;/** * 获取链表的长度 */@Overridepublic int size() {int count = 0;if (head == null) {return count;} else {count++;Node node = head.getNext();while (node != null) {count++;node = node.getNext();}}return count;}/** * 得到指定位置上的元素 */@Overridepublic Node get(int index) {// 如果索引小于零的时候就输出异常if (index < 0) {try {throw new Exception("索引不能为负数" + index);} catch (Exception e) {e.printStackTrace();return null;}}// 定义一个计数器int count = 0;// 当索引值等于零的时候,直接返回头节点;实现下标从零开始if (count == index) {return head;}// 判断下标是否越界   长度是从1开始;索引是从0开始;索引是客户输入的if (index < size()) {Node node = head.getNext();while (node != null) {count++;if (count == index) {return node;}node = node.getNext();}} else {try {throw new Exception("下标越界");} catch (Exception e) {e.printStackTrace();}}return null;}/** * 在链表上添加元素 */@Overridepublic void add(String value) {Node node = new Node(value, null);if (size() == 0) {head = node;} else {Node node1 = get(size() - 1);node1.setNext(node);}}/** * 在指定位置添加元素 *  */@Overridepublic void add(String value, int index) {// 链表添加节点// 创建一个新的节点Node node = new Node(value, null);if(head==null){head=node;return;}//先拿到当前索引位置的节点if(index==0){node.setNext(head);head = node;return;}Node nodeCurrent = get(index);Node nodeParent = get(index-1);nodeParent.setNext(node);node.setNext(nodeCurrent);}/** * 移除指定位置的元素 */@Overridepublic void remove(int index) {int count = size();<span style="white-space:pre"></span>Node temp = get(index);<span style="white-space:pre"></span>if (temp != null) {<span style="white-space:pre"></span>temp = head.getNext();<span style="white-space:pre"></span>Node next = temp.getNext();<span style="white-space:pre"></span>head.setNext(next);<span style="white-space:pre"></span>count--;<span style="white-space:pre"></span>}}}
四、测试自己的链表:
public class Test {   public static void main(String[] args) {MyLinkedList list = new MyLinkedList();list.add("节点1");list.add("节点2");list.add("节点3");list.add("节点4");System.out.println(list.size());System.out.println(list.get(0).getDate());System.out.println(list.get(1).getDate());System.out.println(list.get(2).getDate());System.out.println(list.get(3).getDate());
                list.remove(1);<span style="white-space:pre"></span>System.out.println(list.size());<span style="white-space:pre"></span>System.out.println(list.get(0).getDate());<span style="white-space:pre"></span>System.out.println(list.get(1).getDate());<span style="white-space:pre"></span>System.out.println(list.get(2).getDate());}}

2、双链表的实现
public class SxtLinkedList {public Note first;public Note last;public int size;//删除指定位置的结点public void remove(int index) {Note temp = Note(index);if (temp != null) {Note up = temp.previous;Note down = temp.next;up.next = down;down.previous = up;size--;}     }   //得到一个结点private Note Note(int index) {Note temp = null;if (first != null) {temp = first;for (int i = 0; i < index; i++) {temp = temp.next;}}return temp;}    //增加元素public void add(Object obj) {Note n = new Note();if (first == null) {n.setPrevious(null);n.setObj(obj);n.setNext(null);first = n;last = n;} else {n.setPrevious(last);n.setObj(obj);n.setNext(null);last.setNext(n);last = n;}size++;}public int size() {return size;}public static void main(String[] args) {SxtLinkedList list = new SxtLinkedList();list.add("aaa");list.add("bbb");list.add("ccc");System.out.println(list.size());list.remove(1);System.out.println(list.size());}}



0 0
原创粉丝点击