Bidirectional linked list of data structures (Java implementation)

Source: Internet
Author: User

Welcome reprint, please attach Source:
http://blog.csdn.net/as02446418/article/details/47114711
Recently re-review some of the basic data structure, found themselves a lot has been forgotten, simply pick up the previous knowledge, today I reviewed the list of knowledge, with Java implementation of a two-way linked list, the following look at the code:

 Public classdoublelinkedlist {//Nodes Class node  Private Static classNode {Objectvalue; Node prev = This; Node next = This; Node (Object v) {value= V; } PublicStringtoString()      {return value. toString (); }    }PrivateNode head =NewNode (NULL);//Head node  Private intSize//List size  //The following is an interface method  //Add to Linked list header   PublicBooleanAddFirst(Object o) {Addafter (NewNode (o), head);return true; }//Add elements to the footer of the list   PublicBooleanAddLast(Object o) {Addbefore (NewNode (o), head);return true; } PublicBooleanAdd(Object o) {returnAddLast (o); }//Add elements to the specified location   PublicBooleanAdd(intIndex, Object o) {addbefore (NewNode (o), GetNode (index));return true; }//Remove the specified location   PublicBooleanRemove(intIndex) {removeNode (GetNode (index));return true; }//Remove linked list header elements   PublicBooleanRemovefirst() {RemoveNode (head.next);return true; }//Remove linked list footer elements   PublicBooleanRemovelast() {RemoveNode (Head.prev);return true; }//Take the element value at the specified location   PublicObjectGet(intIndex) {returnGetNode (Index).value; }//Returns the size of the linked list   Public int size()    {returnSize } PublicStringtoString() {StringBuffer s =NewStringBuffer ("["); Node node = head; for(inti =0; i < size; i++) {node = Node.next;if(I >0) S.append (", "); S.append (node.value); } s.append ("]");returnS.tostring (); }//The following is the implementation method  //Find linked list elements  PrivateNodeGetNode(intIndex) {if(Index <0|| Index >= size)Throw NewIndexoutofboundsexception (); Node node = head.next; for(inti =0; I < index; i++) node = Node.next;returnNode }//Add elements before an element  Private void Addbefore(Node NewNode, node node)    {Newnode.prev = Node.prev;    Newnode.next = node;    NewNode.next.prev = NewNode;    NewNode.prev.next = NewNode;  size++; }//Add elements after an element  Private void Addafter(Node NewNode, node node)      {newnode.prev = node;      Newnode.next = Node.next;      NewNode.next.prev = NewNode;      NewNode.prev.next = NewNode;  size++; }//Remove specific elements  Private void RemoveNode(Node node)    {Node.next.prev = Node.prev;    Node.prev.next = Node.next; Node.prev =NULL; Node.next =NULL;  size--; }  }//Some areas can also be optimized, such as finding whether the index is larger than half the size, and if so, start iterating from the other end. 

Test class:

 Public classTest { Public Static void Main(string[] args) {Doublelinkedlist dll =NewDoublelinkedlist ();//AddDll.add ("A"); Dll.add ("B"); Dll.add ("C"); System. out. println (DLL);//Add to the frontDll.addfirst ("D"); System. out. println (DLL);//Add to lastDll.addlast ("E"); System. out. println (DLL);//Add to specified locationDll.add (4,"F"); System. out. println (DLL);//Remove the frontDll.removefirst (); System. out. println (DLL);//Remove the lastDll.removelast (); System. out. println (DLL);//Remove at the specified locationDll.remove (2); System. out. println (DLL);//Returns the element at the specified positionSystem. out. println (DLL.Get(1)); }  }

The Java implementation of the linked list and C + + is a bit different from the C + + use of a pointer, and Java is a reference, here the difference I hope you pay attention to.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bidirectional linked list of data structures (Java implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.