1. Create a node node for a doubly linked list
2. Establish the class LinkedList, the member head is a reference to the table header, define the operation method of the list in LinkedList.
PS: Insert delete operation to determine the POS, if at the end of the table, to be special to prevent the occurrence of null references.
Public classNode {//Create a node for a doubly linked list PublicNode left; PublicNode right; Public intdata; PublicNode () { This. left=NULL; This. right=NULL; This. data=0; } PublicNode (intdata) { This. data=data; } }
Public classLinkedList { PublicNode head;//Reference to header address Public intlength; PublicLinkedList () {//Establish head node length=0; Head=NewNode (); Head.data=0; Head.left=NULL; Head.right=NULL; } Public BooleanInsert (intPos,node Node) {//Insert node node between nodes labeled Pos-1 and POSintCount=0; Node Prenode=Head; while(prenode!=NULL&&count<pos-1) { ++count; Prenode=Prenode.right; } if(prenode==NULL|| Count!=pos-1){ return false; } if(prenode.right==NULL) {Prenode.right=node; Node.left=Prenode; Node.right=NULL; ++length; return true; } Node NextNode=Prenode.right; Node.right=NextNode; Node.left=Prenode; Prenode.right=node; Nextnode.left=node; ++length; return true; } Public BooleanDelete (intPOS) {//delete nodes at the specified locationif(pos<0| | pos>=length) { return false; } intCount=0; Node Curnode=Head; while(count!=pos&&curnode!=NULL) { ++count; Curnode=Curnode.right; } if(count!=pos| | curnode==NULL) { return false; } if(curnode.right==NULL) {CurNode.left.right=NULL; Curnode=NULL; } if(curnode.right!=NULL) {//beforecurnode.left.right=Curnode.right; CurNode.right.left=Curnode.left; } --length; return true; } Public intSize () {//returns the number of linked list elementsreturnlength; } Public BooleanIsEmpty () {if(length==0) { return true; } return false; } Public BooleanTraverse () {//Output list Node Prenode=Head; while(prenode.right!=NULL) {Prenode=Prenode.right; System.out.print (Prenode.data+" "); } return true; } Public intGetData (intPOS) {//Get specified position node element node Prenode=Head; intCount=0; while(count!=POS) {Prenode=Prenode.right; ++count; } returnPrenode.data; } Public BooleanDelete (node node) {//Delete node node Curnode=Head; while(curnode!=node&&curnode!=NULL) {Curnode=Curnode.right; } if(curnode!=node) { return false; } if(curnode.right!=NULL) {CurNode.left.right=Curnode.right; CurNode.right.left=Curnode.left; --length; return true; } if(curnode.right==NULL) {CurNode.left.right=NULL; --length; return true; } return false; }}
Public classMain { Public Static voidMain (string[] args) {LinkedList link=NewLinkedList (); //System.out.println (link.head==null);Node node1=NewNode (1); Node Node2=NewNode (3); Node Node3=NewNode (5); Node Node4=NewNode (9); Node Node5=NewNode (2); BooleanF=link. Insert (1, Node1); F=link. Insert (2, Node2); F=link. Insert (3, NODE3); F=link. Insert (4, NODE4); F=link. Insert (5, NODE5); //System.out.println (Link.head.data); //System.out.println (Link.head.right.data); //System.out.println (f); //Node node=node1;Link. Delete (2); Link. Delete (NODE5); Booleang=link. Delete (NODE3); //System.out.println ("g=" +g); //While (node!=null) {//System.out.println (Node.data); //Node=node.right; // }System.out.println ("size=" +link. Size ()); System.out.println ("IsEmpty?" +link.isempty ()); Link. Traverse (); System.out.println (); System.out.println ("Data2=" +link.getdata (2)); }}
Java bidirectional linked list INSERT, delete, find operation with header