1 package DataStructure.main1;
2 3 public abstract class AbstractList<E> implements List<E> { 4 protected int size; 5 6 protected void rangeCheck(int index) { 7 if(index < 0 || index >= size) { 8 outOfBounds(index); 9 } 10 } 11 12 protected void rangeCheckForAdd(int index) { 13 if(index < 0 || index > size) { 14 outOfBounds(index); 15 } 16 } 17 18 protected void outOfBounds(int index) { 19 throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); 20 } 21 22 public int size() { 23 return size; 24 } 25 26 public boolean isEmpty() { 27 return size == 0; 28 } 29 30 public boolean contains(E element) { 31 return indexOf(element) != ELEMENT_NOT_FOUND; 32 } 33 34 public void add(E element) { 35 add(size, element); 36 } 37 38 }
2.接口
1 package DataStructure.main1; 2 3 public interface List <E>{ 4 //-1下标:代表没有这个元素 5 static final int ELEMENT_NOT_FOUND = -1; 6 7 void clear(); 8 9 int size(); 10 11 boolean isEmpty(); 12 13 boolean contains(E element); 14 15 void add(E element); 16 17 E get(int index); 18 19 E set(int index, E element); 20 21 void add(int index, E element); 22 23 E remove(int index); 24 25 int indexOf(E element); 26 27 }
3.普通单向链表
1 package DataStructure.Linkedlist; 2 3 import DataStructure.main1.AbstractList; 4 5 public class SingleLinkedList<E> extends AbstractList<E> { 6 private Node<E> first; 7 8 //内部类:Node节点 9 private static class Node<E>{ 10 E elemnt; 11 Node next; 12 public Node(E element, Node<E> next){ 13 this.elemnt = element; 14 this.next = next; 15 } 16 } 17 18 @Override 19 public void clear() { 20 size=0; 21 first=null; 22 } 23 24 @Override 25 public E get(int index) { 26 return node(index).elemnt; 27 } 28 29 @Override 30 public E set(int index, E element) { 31 Node<E> node = node(index); //取得index位置的node引用 32 E old = node.elemnt; //把当前index位置的element先保留 33 element=node.elemnt; //新的element替换旧的 34 return old; //返回之前的old 35 } 36 37 @Override 38 public void add(int index, E element) { 39 if(index==0) 40 { 41 first=new Node<E>(element,first);//*********** 42 } 43 else{ 44 Node<E> prev=node(index-1);//上一个节点 45 prev.next= new Node<E>(element,prev.next);//prev指向新加的节点 46 } 47 size++; 48 49 } 50 51 @Override 52 public E remove(int index) { 53 Node<E> node =first; 54 if(index==0){ 55 first=first.next; 56 } 57 else { 58 59 Node<E> prev = node(index - 1);//上一个节点 60 node =prev.next; 61 prev.next = prev.next.next; //node.next 62 63 } 64 size--; 65 return node.elemnt; 66 } 67 68 @Override 69 public int indexOf(E element) { 70 Node<E> tmpNode = first; 71 if(element == null) { 72 for (int i=0; i < size; i++) { 73 if (tmpNode.elemnt == null) return i; 74 tmpNode = tmpNode.next; 75 } 76 }else { 77 //那么element一定不为null,放在前面调用equals绝对没问题 78 for (int i=0; i < size; i++) { 79 if (element.equals(tmpNode.elemnt)) return i; 80 tmpNode = tmpNode.next; 81 } 82 } 83 return ELEMENT_NOT_FOUND; 84 85 } 86 //Node类型, 87 private Node<E> node(int index){ 88 rangeCheck(index); 89 Node<E> node=first;//first是第一个node的引用, 90 for (int i = 0; i <index ; i++) { 91 node=node.next;//找到第i个node 92 } 93 return node; 94 } 95 @Override 96 public String toString() { 97 StringBuilder string = new StringBuilder(); 98 Node<E> node =first; 99 string.append("size").append(size).append(", ["); 100 for (int i = 0; i < size; i++) { 101 if(i != 0) 102 string.append(", "); 103 string.append(node.elemnt); 104 node=node.next; 105 106 } 107 string.append("]"); 108 return string.toString(); 109 } 110 111 112 }
4.增加了虚拟头节点的单向链表
1 package DataStructure.Linkedlist; 2 3 import DataStructure.main1.AbstractList; 4 //增加一个虚拟头节点 5 public class SingleLinkedList2<E> extends AbstractList<E> { 6 private Node<E> first; 7 //头节点 8 public SingleLinkedList2(){ 9 first=new Node<>(null,null); 10 } 11 //内部类:Node节点 12 private static class Node<E>{ 13 E elemnt; 14 Node next; 15 public Node(E element, Node<E> next){ 16 this.elemnt = element; 17 this.next = next; 18 } 19 } 20 21 @Override 22 public void clear() { 23 size=0; 24 first=null; 25 } 26 27 @Override 28 public E get(int index) { 29 return node(index).elemnt; 30 } 31 32 @Override 33 public E set(int index, E element) { 34 Node<E> node = node(index); //取得index位置的node引用 35 E old = node.elemnt; //把当前index位置的element先保留 36 element=node.elemnt; //新的element替换旧的 37 return old; //返回之前的old 38 } 39 40 @Override 41 public void add(int index, E element) { 42 rangeCheckForAdd(index); 43 Node<E> prev =index ==0 ?first:node(index-1); 44 //Node<E> prev=node(index-1);//上一个节点 45 prev.next= new Node<E>(element,prev.next);//prev指向新加的节点 46 47 size++; 48 49 } 50 51 @Override 52 public E remove(int index) { 53 rangeCheck(index); 54 Node<E> prev =index ==0 ?first:node(index-1); 55 56 // Node<E> node =first; 57 // Node<E> prev = node(index - 1);//上一个节点 58 Node<E> node =prev.next; 59 prev.next = prev.next.next; //node.next 60 61 62 size--; 63 return node.elemnt; 64 } 65 66 @Override 67 public int indexOf(E element) { 68 Node<E> tmpNode = first; 69 if(element == null) { 70 for (int i=0; i < size; i++) { 71 if (tmpNode.elemnt == null) return i; 72 tmpNode = tmpNode.next; 73 } 74 }else { 75 //那么element一定不为null,放在前面调用equals绝对没问题 76 for (int i=0; i < size; i++) { 77 if (element.equals(tmpNode.elemnt)) return i; 78 tmpNode = tmpNode.next; 79 } 80 } 81 return ELEMENT_NOT_FOUND; 82 83 } 84 //Node类型, 85 private Node<E> node(int index){ 86 rangeCheck(index); 87 Node<E> node=first.next;//first是第一个node的引用, 88 for (int i = 0; i <index ; i++) { 89 node=node.next;//找到第i个node 90 } 91 return node; 92 } 93 @Override 94 public String toString() { 95 StringBuilder string = new StringBuilder(); 96 Node<E> node =first.next; 97 string.append("size").append(size).append(", ["); 98 for (int i = 0; i < size; i++) { 99 if(i != 0) 100 string.append(", "); 101 string.append(node.elemnt); 102 node=node.next; 103 104 } 105 string.append("]"); 106 return string.toString(); 107 } 108 109 110 }
5.双向链表
1 package DataStructure.Linkedlist; 2 3 import DataStructure.main1.AbstractList; 4 //*****************************************重要 5 public class DoubleLinkedList<E> extends AbstractList<E> { 6 private Node<E> first; 7 private Node<E> last; 8 9 //内部类:Node节点 10 private static class Node<E>{ 11 E elemnt; 12 Node <E> next; 13 Node <E> prev; 14 15 public Node(Node<E> prev,E element, Node<E> next){ 16 this.elemnt = element; 17 this.next = next; 18 this.prev= prev; 19 } 20 } 21 22 @Override 23 public void clear() { 24 size=0; 25 first=null; 26 last=null; 27 } 28 29 @Override 30 public E get(int index) { 31 return node(index).elemnt; 32 } 33 34 @Override 35 public E set(int index, E element) { 36 Node<E> node = node(index); //取得index位置的node引用 37 E old = node.elemnt; //把当前index位置的element先保留 38 element=node.elemnt; //新的element替换旧的 39 return old; //返回之前的old 40 } 41 42 @Override 43 public void add(int index, E element) { 44 rangeCheckForAdd(index); 45 if(index==size){ 46 Node<E> oldlast =last; 47 last=new Node<E>(oldlast,element,null); 48 if(oldlast==null){ 49 //原来是空 50 first=last; 51 }else { 52 oldlast.next=last;} 53 }else { 54 Node<E> next = node(index); //当前的index在等会添加完之后是新添加元素的next 55 Node<E> prev = next.prev; 56 Node<E> node = new Node<E>(prev, element, next); 57 next.prev = node; 58 if (prev == null) { 59 first = node; 60 } else { 61 prev.next = node; 62 } 63 } 64 // //特殊情况,添加到0或者last 65 size++; 66 67 } 68 69 @Override 70 public E remove(int index) { 71 Node<E> node=node(index); 72 Node<E> prev=node.prev; 73 Node<E> next=node.next; 74 if(prev==null){//index==0 75 first=next; 76 }else{ 77 prev.next=next;} 78 if(next==null){//index==size-1 79 last=prev; 80 }else { 81 next.prev=prev;} 82 size--; 83 return node.elemnt; 84 } 85 86 @Override 87 public int indexOf(E element) { 88 Node<E> tmpNode = first; 89 if(element == null) { 90 for (int i=0; i < size; i++) { 91 if (tmpNode.elemnt == null) return i; 92 tmpNode = tmpNode.next; 93 } 94 }else { 95 //那么element一定不为null,放在前面调用equals绝对没问题 96 for (int i=0; i < size; i++) { 97 if (element.equals(tmpNode.elemnt)) return i; 98 tmpNode = tmpNode.next; 99 } 100 } 101 return ELEMENT_NOT_FOUND; 102 103 } 104 //Node类型, 105 private Node<E> node(int index){ 106 rangeCheck(index); 107 Node<E> node=null; 108 if(index<(size>>1)){ 109 node=first;//first是第一个node的引用, 110 for (int i = 0; i <index ; i++) { 111 node = node.next;//找到第i个node 112 } 113 } 114 else { 115 node=last;//first是第一个node的引用, 116 for (int i = size-1; i >index ; i--) { 117 node=node.prev;//找到第i个node 118 } 119 } 120 return node; 121 } 122 @Override 123 public String toString() { 124 StringBuilder string = new StringBuilder(); 125 Node<E> node =first; 126 string.append("size").append(size).append(", ["); 127 for (int i = 0; i < size; i++) { 128 if(i != 0) 129 string.append(", "); 130 string.append(node.elemnt); 131 node=node.next; 132 133 } 134 string.append("]"); 135 return string.toString(); 136 } 137 138 139 }
6.单向循环链表
1 package DataStructure.Linkedlist; 2 3 import DataStructure.main1.AbstractList; 4 5 public class CycleSingleLinkedList<E> extends AbstractList<E> { 6 private Node<E> first; 7 8 //内部类:Node节点 9 private static class Node<E>{ 10 E elemnt; 11 Node next; 12 public Node(E element, Node<E> next){ 13 this.elemnt = element; 14 this.next = next; 15 } 16 } 17 18 @Override 19 public void clear() { 20 size=0; 21 first=null; 22 } 23 24 @Override 25 public E get(int index) { 26 return node(index).elemnt; 27 } 28 29 @Override 30 public E set(int index, E element) { 31 Node<E> node = node(index); //取得index位置的node引用 32 E old = node.elemnt; //把当前index位置的element先保留 33 element=node.elemnt; //新的element替换旧的 34 return old; //返回之前的old 35 } 36 37 @Override 38 public void add(int index, E element) { 39 if(index==0) 40 { 41 Node<E> newfirst=new Node<E>(element,first);//*********** 42 43 Node<E> last=(size==0)?newfirst:node(size-1);//range 44 last.next=first; 45 first=newfirst; 46 } 47 else{ 48 Node<E> prev=node(index-1);//上一个节点 49 prev.next= new Node<E>(element,prev.next);//prev指向新加的节点 50 } 51 size++; 52 53 } 54 55 @Override 56 public E remove(int index) { 57 Node<E> node =first; 58 if(index==0){//特殊情况,删除第一个节点,或者只有一个节点 59 if(size==1) 60 {first=null;} 61 else { 62 Node<E> last = node(size - 1); 63 first = first.next; 64 last.next = first; 65 } 66 } 67 else { 68 69 Node<E> prev = node(index - 1);//上一个节点 70 node =prev.next; 71 prev.next = prev.next.next; //node.next 72 73 } 74 size--; 75 return node.elemnt; 76 } 77 78 @Override 79 public int indexOf(E element) { 80 Node<E> tmpNode = first; 81 if(element == null) { 82 for (int i=0; i < size; i++) { 83 if (tmpNode.elemnt == null) return i; 84 tmpNode = tmpNode.next; 85 } 86 }else { 87 //那么element一定不为null,放在前面调用equals绝对没问题 88 for (int i=0; i < size; i++) { 89 if (element.equals(tmpNode.elemnt)) return i; 90 tmpNode = tmpNode.next; 91 } 92 } 93 return ELEMENT_NOT_FOUND; 94 95 } 96 //Node类型, 97 private Node<E> node(int index){ 98 rangeCheck(index); 99 Node<E> node=first;//first是第一个node的引用, 100 for (int i = 0; i <index ; i++) { 101 node=node.next;//找到第i个node 102 } 103 return node; 104 } 105 @Override 106 public String toString() { 107 StringBuilder string = new StringBuilder(); 108 Node<E> node =first; 109 string.append("size").append(size).append(", ["); 110 for (int i = 0; i < size; i++) { 111 if(i != 0) 112 string.append(", "); 113 string.append(node.elemnt); 114 node=node.next; 115 116 } 117 string.append("]"); 118 return string.toString(); 119 } 120 121 122 }
7.双向循环链表
1 package DataStructure.Linkedlist; 2 3 import DataStructure.main1.AbstractList; 4 5 public class CycleDoubleLinkedList<E> extends AbstractList<E> { 6 private Node<E> first; 7 private Node<E> last; 8 9 //内部类:Node节点 10 private static class Node<E>{ 11 E elemnt; 12 Node <E> next; 13 Node <E> prev; 14 15 public Node(Node<E> prev,E element, Node<E> next){ 16 this.elemnt = element; 17 this.next = next; 18 this.prev= prev; 19 } 20 } 21 22 @Override 23 public void clear() { 24 size=0; 25 first=null; 26 last=null; 27 } 28 29 @Override 30 public E get(int index) { 31 return node(index).elemnt; 32 } 33 34 @Override 35 public E set(int index, E element) { 36 Node<E> node = node(index); //取得index位置的node引用 37 E old = node.elemnt; //把当前index位置的element先保留 38 element=node.elemnt; //新的element替换旧的 39 return old; //返回之前的old 40 } 41 42 @Override 43 public void add(int index, E element) { 44 rangeCheckForAdd(index); 45 if(index==size){ 46 Node<E> oldlast =last; 47 last=new Node<E>(oldlast,element,first);//双向链表这里的最后一个节点指向Null,但是双向循环链表这个节点就指向了first 48 if(oldlast==null){ 49 //原来是空 50 first=last; 51 first.next=first; 52 first.prev=first;//自己的下一个指向自己,自己的上一个指向自己 53 }else { 54 oldlast.next=last; 55 first.prev=last; 56 } 57 }else { 58 Node<E> next = node(index); //当前的index在等会添加完之后是新添加元素的next 59 Node<E> prev = next.prev; 60 Node<E> node = new Node<E>(prev, element, next); 61 next.prev = node; 62 prev.next = node; 63 64 if (next==first){//index==0) { 65 first = node; 66 } 67 } 68 // //特殊情况,添加到0或者last 69 size++; 70 71 } 72 73 @Override 74 public E remove(int index) { 75 Node<E> node=node(index); 76 Node<E> prev=node.prev; 77 Node<E> next=node.next; 78 prev.next=next; 79 next.prev=prev; 80 if(node==first){//index==0 81 first=next; 82 } 83 if(node==last){//index==size-1 84 last=prev; 85 } 86 size--; 87 return node.elemnt; 88 } 89 90 @Override 91 public int indexOf(E element) { 92 Node<E> tmpNode = first; 93 if(element == null) { 94 for (int i=0; i < size; i++) { 95 if (tmpNode.elemnt == null) return i; 96 tmpNode = tmpNode.next; 97 } 98 }else { 99 //那么element一定不为null,放在前面调用equals绝对没问题 100 for (int i=0; i < size; i++) { 101 if (element.equals(tmpNode.elemnt)) return i; 102 tmpNode = tmpNode.next; 103 } 104 } 105 return ELEMENT_NOT_FOUND; 106 107 } 108 //Node类型, 109 private Node<E> node(int index){ 110 rangeCheck(index); 111 Node<E> node=null; 112 if(index<(size>>1)){ 113 node=first;//first是第一个node的引用, 114 for (int i = 0; i <index ; i++) { 115 node = node.next;//找到第i个node 116 } 117 } 118 else { 119 node=last;//first是第一个node的引用, 120 for (int i = size-1; i >index ; i--) { 121 node=node.prev;//找到第i个node 122 } 123 } 124 return node; 125 } 126 @Override 127 public String toString() { 128 StringBuilder string = new StringBuilder(); 129 Node<E> node =first; 130 string.append("size").append(size).append(", ["); 131 for (int i = 0; i < size; i++) { 132 if(i != 0) 133 string.append(", "); 134 string.append(node.elemnt); 135 node=node.next; 136 137 } 138 string.append("]"); 139 return string.toString(); 140 } 141 142 143 }