双链表java实现_双链表java实现(二)

/*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */ package arthur.datastruct.programe; import java.util.NoSuchElementException; /**  *  * @author dell  */ public class MyLinkedList {     private int size;     private Node head;//链表头节点     private Node tail;//链表尾节点     public MyLinkedList(int size, Node head, Node tail) {         this.size = size;         this.head = head;     }     public MyLinkedList() {         this.clear();     }     /**      * 清空链表,注意清空链表的时候只剩下首尾两个相连的空节点      */     public void clear() {         this.head = new Node(null, null, null);         this.tail = new Node(null, head, null);         this.size = 0;         this.head.next = this.tail;     }         /**      * 获得第index个节点      * @param index      * @return       */     public Node get(int index) {         return this.getNode(index);     }     public Object getValueOfIndex(int index) {         return this.getNode(index).data;     }     /**      * 删除节点的时候,让removeNode的后继节点的前驱指向removeNode的前驱,      * removeNode几点的后继指向removeNode的后继即可实现删除操作      * @param removeNode      * @return       */     private Object remove(Node removeNode) {         if (null == removeNode) {             throw new NoSuchElementException();         }         removeNode.next.pre = removeNode.pre;         removeNode.pre.next = removeNode.next;         this.size--;         return removeNode.data;     }     /**      * 删除第index个节点,注意在删除最后一个结点的时候判断条件是index == size-1      * 而不是index==size      * @param index      * @return       */     public Node remove(int index) {         Node node = this.get(index);         if(index == this.size-1){             this.tail = node.pre;             this.size--;         }else{              this.remove(node);         }                 return node;     }     /**      * get the length of list      * @return size      */     public int size() {         return size;     }     /**      * whether the list is impty or not      * @return       */     public boolean isEmpty() {         return 0 == size;     }     public Node getHead() {         return head;     }     public void setHead(Node head) {         this.head = head;     }     public Node getTail() {         return tail;     }     public void setTail(Node tail) {         this.tail = tail;     }     /**      * 添加节点,      * @return       */     private boolean addFirst(Node node) {         if (null == node) {             throw new NullPointerException();         }         if (0 == size) {             node.pre = head;             head.next = node;         } else {             node.pre = tail;             tail.next = node;         }         tail = node;//让尾结点指向刚添加的那个点,这一点至关重要         this.size++;         return true;     }     private void add(Node node) {         this.addFirst(node);     }     /**      * 添加值为Object的结点,注意要把结点封装到内部类Node里面      * @param object       */     public void add(Object object) {         this.add(new Node(object, null, null));     }     /**      * 获得第index个结点      * @param index      * @return       */     private Node getNode(int index) {         Node node = null;         if (index < 0 || index > this.size()) {             throw new IndexOutOfBoundsException();         }         //折半的形式查找,如果index不大于二分之一的链表长度,从头开始遍历         if (index <= this.size() >> 1) {             node = this.head.next;             for (int i = 0; i < index; i++) {                 node = node.next;             }         } else {//如果index大于二分之一的链表长度,从尾部开始遍历             node = this.tail;             for (int i = size() - 1; i > index; i--) {                 node = tail.pre;             }         }         return node;     }     /**      * 在第index位置加入结点      * @param index      * @param node       */     private void add(int index, Node node) {         if (index < 0 || index > size()) {             throw new IndexOutOfBoundsException();         }         if (index == size()) {//如果index等于size,即在尾部加入             this.add(node);         } else if (0 == index) {//如果0==index在首部加入             node.pre = head;             node.next = head.next;             head.next = node;             head.next.pre = node;         } else {//在链表中间插入             //获得index之前的那个结点             Node nodeBeforeIndex = this.get(index - 1);             node.pre = nodeBeforeIndex;             node.next = nodeBeforeIndex.next;             nodeBeforeIndex.next = node;             nodeBeforeIndex.next.pre = node;         }         this.size++;     }     /**      * 在第index位置插入值为Object的结点      * @param index      * @param object       */     public void add(int index, Object object) {         this.add(index, new Node(object, null, null));     }          /**      * 结点类,为内部类      * @param        */      private class Node {         public Object data;//节点数据         public Node pre;//前驱节点         public Node next;//后继节点         public Node(Object data, Node pre, Node next) {             this.data = data;             this.pre = pre;             this.next = next;         }         public Node() {         }         public String toString() {             return data + "";         }     } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值