双链表
- 1.复杂方法的图分析
- 2.My_LinkedList代码
- 3.接口MY_lIST
- 4.测试类
1.复杂方法的图分析
2.My_LinkedList代码
package My_liNKEDlIST;public class My_LinkedList implements MY_lIST{static class ListNode{public int val;public ListNode prev;public ListNode next;public ListNode(int val){this.val = val;}}public ListNode head;public ListNode last;@Overridepublic void addFirst(int data) {ListNode node = new ListNode(data);if(head == null){head = node;last = node;}else{node.next = head;head.prev = node;head = node;}}@Overridepublic void addLast(int data) {ListNode node = new ListNode(data);if(head == null){head = node;last = node;}else{last.next = node;node.prev = last;last = node;}}@Overridepublic void addIndex(int index, int data) throws IndexOutOFException{int len = size();if(index < 0 || index > len){throw new IndexOutOFException("越界了");}if(index == 0){addFirst(data);return;}if(index == len){addLast(data);return;}ListNode cur = head;ListNode node = new ListNode(data);while(index != 0){cur = cur.next;index--;}cur.prev.next = node;node.next = cur;node.prev = cur.prev;cur.prev = node;}@Overridepublic boolean contains(int key) {ListNode cur = head;while(cur != null){if(cur.val == key){return true;}cur = cur.next;}return false;}@Overridepublic void remove(int key) {ListNode cur = head;while(cur != null){if(cur.val == key){//如果删除的结点为头结点if(cur == head){head = head.next;if(head != null) {head.prev = null;}}else {if(cur == last){cur.prev.next = cur.next;last = last.prev;}else {cur.prev.next = cur.next;cur.next.prev = cur.prev;}}return;}else{cur = cur.next;}}}@Overridepublic void removeAllKey(int key) {ListNode cur = head;while(cur != null){if(cur.val == key){//如果删除的结点为头结点if(cur == head){head = head.next;if(head != null) {head.prev = null;}}else {if(cur.next == null) {cur.prev.next = cur.next;}else {cur.next.prev = cur.prev;}}}cur = cur.next;}}@Overridepublic int size() {ListNode cur = head;int count = 0;while(cur != null){count++;cur = cur.next;}return count;}@Overridepublic void display() {ListNode cur = head;while(cur != null){System.out.println(cur.val+" ");cur = cur.next;}}@Overridepublic void clear() {ListNode cur = head;while(cur != null){ListNode tmp = cur.next;cur.prev = null;cur.next = null;cur = tmp;}this.head = null;this.last = null;}
}
3.接口MY_lIST
public interface MY_lIST {// 2、无头双向链表实现//头插法public void addFirst(int data);//尾插法public void addLast(int data);//任意位置插入,第一个数据节点为0号下标public void addIndex(int index,int data);//查找是否包含关键字key是否在单链表当中public boolean contains(int key);//删除第一次出现关键字为key的节点public void remove(int key);//删除所有值为key的节点public void removeAllKey(int key);//得到单链表的长度public int size();public void display();public void clear();}
4.测试类
public class Test {public static void main(String[] args) {My_LinkedList LIST = new My_LinkedList();LIST.addFirst(1);LIST.addFirst(1);LIST.addFirst(1);//LIST.display();LIST.addLast(4);LIST.addLast(5);LIST.addLast(6);//LIST.display();LIST.addIndex(4,7);//LIST.display();//LIST.clear();//LIST.display();//LIST.remove(4);LIST.removeAllKey(1);LIST.display();}
}
双链表的思路和单链表大同小异,就是多加了一个前驱指针,希望大家好好学习双链表的代码