双向链表(DoubleLinkedList)
双向链表多了个Pre指针。
在链表尾部添加节点:
//add new node in the linked list end public void add(Node node) { Node temp = headNode; while(true) { if(temp.next == null) { temp.next = node; node.pre = temp; break; }else { temp = temp.next; } } }
更新节点:(此方法和单链表的更新节点方法一样)
//update node public void update(Node node) { if(headNode.next == null) { System.out.println("Empty double linked list!"); } Node temp = headNode.next; boolean isFind = false; while(!isFind) { if(temp == null) { System.out.printf("Cannot find the node: %d",node.no); break; } else if(temp.no == node.no) { temp.name = node.name; isFind = true; }else { temp = temp.next; } } }
删除节点:
public void deleteNode(int no) { Node temp = headNode.next; if(temp ==null) { System.out.println("The double linked list is empty!"); } boolean isFind = false; while(!isFind) { if(temp == null) { System.out.printf("Cannot find the node: %d",no); break; }else if(temp.no ==no) { temp.pre.next = temp.next; if(temp.next!=null) { temp.next.pre = temp.pre; } isFind = true; }else { temp = temp.next; } } }
循环链表:
//show the double linked list public void list() { Node temp = headNode.next; if(temp == null) { System.out.println("The double linked list is empty!"); } while(temp!=null) { System.out.println(temp); temp = temp.next; } }
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号