Java data structure: doubly linked list

Source: Internet
Author: User

The difference between a doubly linked list and a single-linked list is that he has one more domain to store the precursor nodes.

Basic method implementation:

New node:

The next of the current node is the new node, and the new node's next is the current node's next.next new node, the prior of the current nodes is the current node, and the prior of the nodes is new.

To delete a node:

Sets the previous node of the delete node as the current junction. If the next node of the current junction is present, the next node of the current junction is the Next.next of the current node, and the prior of the current nodes is the current node, otherwise the next=null of the current node


Realize:

Use doubly linked lists to enable students to add, delete, output, find

Create Student class: Student.java

 PackageDoublelink;/** * @authoren * Student class*/ Public classStudent {//School Number    Private intsId; //name    PrivateString SName; //Age    Private intSAge; //class    PrivateString ClassName;  Public intGetSID () {returnsId; }     Public voidSetsid (intsId) {         This. SId =sId; }     PublicString Getsname () {returnSName; }     Public voidsetsname (String sName) { This. SName =SName; }     Public intGetsage () {returnSAge; }     Public voidSetsage (intSAge) {         This. SAge =SAge; }     PublicString GetClassName () {returnClassName; }     Public voidsetclassname (String className) { This. ClassName =ClassName; }     PublicStudent (intSId, String SName,intSAge, String className) {         This. SId =sId;  This. SName =SName;  This. SAge =SAge;  This. ClassName =ClassName; } @Override PublicString toString () {return"student{" + "sid=" + SId + ", sname=" + sName + "\" + ", sage=" + SAge + ", classname= '" + className + ' + ' + '} '; }}

Create node class: Dnode.java

 PackageDoublelink;/** * @authorzh * Doubly linked list node * Unlike a one-way linked list node, the two-way linked list node has one more chain domain, which is used to store the precursor node*/ Public classDnode {//precursor nodeDnode Prior; //data fieldsStudent Student; //successor nodeDnode Next; //construct the head node.     PublicDnode () { This. Prior =NULL;  This. Student =NULL;  This. Next =NULL; }    //Constructing Nodes     PublicDnode (Student Student) { This. Prior =NULL;  This. Student =student;  This. Next =NULL; }}

Create linked list: Dlink.java

 PackageDoublelink;/*** bidirectional linked list*/ Public classDLink {//Creating a head node    PrivateDnode Head; //Initialize     PublicDLink () {head=NewDnode (); }    //new Node     Public voidaddstudent (Student stu) {Dnode temp=Head; Dnode Data=NewDnode (STU);  while(Temp.next! =NULL) {Temp=Temp.next; } Data.prior=temp; Temp.next=data; }    //ordered New     Public voidAddstudentbysort (Student stu) {Dnode temp=Head; Dnode node=NewDnode (STU);  while(Temp.next! =NULL){            if(Node.student.getsAge () <Temp.next.student.getsAge ()) {Temp.next.prior=node; Node.next=Temp.next; Node.prior=temp; Temp.next=node; return; } temp=Temp.next; } Node.prior=temp; Temp.next=node; }    //double linked list printed by go back     Public voidDisplayheadtolast () {Dnode temp=Head;  while(Temp.next! =NULL) {System.out.println (temp.next.student); Temp=Temp.next; }    }    //get the last node of a list     Publicdnode getlastnodevalue (DLink link) {dnode node=NULL; Dnode Temp=Link.head; if(Link.head.next = =NULL){            return NULL; }         while(Temp.next! =NULL) {Temp=Temp.next; } node=temp; returnnode; }    //Delete a node     Public voiddelstudent (String sName) {Dnode temp=Head;  while(Temp.next! =NULL){            if(Temp.next.student.getsName (). Equals (SName)) {if(Temp.next.next! =NULL) {Temp.next.next.prior=temp; Temp.next=Temp.next.next; }Else{Temp.next=NULL; }                 Break; } temp=Temp.next; }    }     Public Static voidMain (string[] args) {DLink DLink=NewDLink (); Dlink.addstudent (NewStudent (1, "AA", 21, "1")); Dlink.addstudent (NewStudent (1, "BB", 20, "2")); Dlink.addstudent (NewStudent (1, "CC", 24, "3")); Dlink.addstudent (NewStudent (1, "DD", 22, "4")); Dlink.addstudentbysort (NewStudent (1, "AA", 21, "1")); Dlink.addstudentbysort (NewStudent (2, "BB", 20, "2")); Dlink.addstudentbysort (NewStudent (3, "CC", 24, "3")); Dlink.addstudentbysort (NewStudent (4, "DD", 22, "4")); Dlink.delstudent ("CC");        Dlink.displayheadtolast ();        System.out.println (); Student Stu=Dlink.getlastnodevalue (dLink). Student;        System.out.println (Stu); System.out.println ("-----------Reverse output------------"); //Reverse Output//Get last nodeDnode node =Dlink.getlastnodevalue (DLink);  while(Node.prior! =NULL) {System.out.println (node.student); Node=Node.prior; }    }}

Java data structure: doubly linked list

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.