首页 > 代码库 > 自己写的一个双向链表容器

自己写的一个双向链表容器

  1 package com.lovo;  2   3 public class SuperLinkedList <T>{  4     private int size;  5     private SuperLinkedListNode<T> firstNode;  6     private SuperLinkedListNode<T> lastNode;  7     /**  8      * 添加元素(添加在链表的末尾)  9      * @param o 10      */ 11     public void add(T o) { 12         SuperLinkedListNode<T> node = new SuperLinkedListNode<T>(); 13         if (size == 0) { 14             node.setContent(o); 15             node.setNextNode(null); 16             node.setLastNode(null); 17             firstNode = node; 18             lastNode = node; 19             size++; 20         }else { 21             if (!isRepeat(o)) { 22                 node.setContent(o); 23                 node.setNextNode(null); 24                 node.setLastNode(lastNode); 25                 lastNode.setNextNode(node); 26                 lastNode = node; 27                 size++; 28             } 29         } 30     } 31     /** 32      * 插入元素 33      * @param o 元素值 34      * @param index 元素位置 35      */ 36     public void insert(T o,int index) { 37         SuperLinkedListNode<T> node = new SuperLinkedListNode<T>(); 38         if (index >= size || index < 0) { 39             throw new ArrayIndexOutOfBoundsException(); 40         }else { 41             SuperLinkedListNode<T> tempNode = firstNode; 42             for (int i = 0; i < index; i++) { 43                 tempNode = tempNode.getNextNode(); 44             } 45             if (!isRepeat(o)) { 46                 node.setContent(o); 47                 node.setNextNode(tempNode); 48                 if (index != 0) { 49                     SuperLinkedListNode<T> lastNode = tempNode.getLastNode(); 50                     node.setLastNode(lastNode); 51                     lastNode.setNextNode(node); 52                     tempNode.setLastNode(node); 53                 } else {//插入的是头元素 54                     node.setLastNode(null); 55                     tempNode.setLastNode(node); 56                     firstNode = node; 57                 } 58                 size++; 59             } 60         } 61     } 62     /** 63      * 按索引取元素 64      * @param index 下标值 65      * @return 下标值对应的元素 66      */ 67     public SuperLinkedListNode<T> get(int index) { 68         if (index < 0 || index >= size) { 69             throw new ArrayIndexOutOfBoundsException(); 70         } 71         SuperLinkedListNode<T> node = firstNode; 72         for (int i = 0; i < index; i++) { 73             node = node.getNextNode(); 74         } 75         return node; 76     } 77     /** 78      * 通过索引删除元素 79      * @param index 索引值 80      */ 81     public void removeAt(int index) { 82         if (index < 0 || index >= size) { 83             throw new ArrayIndexOutOfBoundsException(); 84         } 85         SuperLinkedListNode<T> node = firstNode; 86         for (int i = 0; i < index; i++) { 87             node = node.getNextNode(); 88         } 89         SuperLinkedListNode<T> nextNode = node.getNextNode(); 90         SuperLinkedListNode<T> lastNode = node.getLastNode(); 91         if (nextNode == null) { 92             if (lastNode != null) {//size为1 93                 node.clear(); 94                 lastNode.setNextNode(null); 95                 this.lastNode = lastNode; 96             } 97         }else { 98             if (lastNode == null) { 99                 nextNode.setLastNode(null);100                 firstNode = nextNode;101             }else {102                 lastNode.setNextNode(nextNode);103                 nextNode.setLastNode(lastNode);104             }105         }106         node.clear();107         size--;108     }109     /**110      * 通过元素值删除元素111      * @param o 删除的元素112      */113     public void remove(T o) {114         if (o != null) {115             SuperLinkedListNode<T> node = firstNode;116             for (int i = 0; i < size; i++) {117                 if (o.equals(node.getContent())) {118                     removeAt(i);119                 }120                 node = node.getNextNode();121             }122         }123     }124     /**125      * 清空容器126      */127     public void clear() {128         SuperLinkedListNode<T> node = firstNode;129         for (int i = 0; i < size - 1; i++) {130             SuperLinkedListNode<T> tempNode = node.getNextNode();131             node.clear();;132             node = tempNode;133         }134         node.clear();135         size = 0;136     }137     /**138      * 得到容器大小139      * @return140      */141     public int size() {142         return size;143     }144     /**145      * 判断元素值是否重复(已经存在)146      * @param o 要比较的值147      * @return true:相同 false:不相同148      */149     private boolean isRepeat(T o){150         SuperLinkedListNode<T> tempNode = firstNode;151         boolean flag = false;152         for (int i = 0; i < size; i++) {153             if(o.equals(tempNode.getContent())){154                 flag = true;155             }156             tempNode = tempNode.getNextNode();157         }158         return flag;159     }160 }
 1 package com.lovo; 2  3 public class  SuperLinkedListNode<T>{ 4     private SuperLinkedListNode<T> nextNode; 5     private SuperLinkedListNode<T> lastNode; 6     private T content; 7     /** 8      * 清空节点 9      */10     public void clear() {11         nextNode = null;12         lastNode = null;13         content = null;14     }15     public SuperLinkedListNode<T> getNextNode() {16         return nextNode;17     }18     public void setNextNode(SuperLinkedListNode<T> nextNode) {19         this.nextNode = nextNode;20     }21     public SuperLinkedListNode<T> getLastNode() {22         return lastNode;23     }24     public void setLastNode(SuperLinkedListNode<T> lastNode) {25         this.lastNode = lastNode;26     }27     public T getContent() {28         return content;29     }30     public void setContent(T content) {31         this.content = content;32     }33     @Override34     public String toString() {35         return "SuperLinkedListNode [content=" + content + "]";36     }37     38 }

 

自己写的一个双向链表容器