Java bidirectional linked list INSERT, delete, find operation with header

Source: Internet
Author: User

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

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.