Java implements the basic operation of the bidirectional circular list

Source: Internet
Author: User
Tags prev

1  PackageEric.adt;2 3 /**4 * <p>5 * Title:doublelinkedlist6 * </p>7 * <p>8 * Description: Using Java to realize the basic operation of two-way circular linked list9 * </p>Ten * <p> One * Location:frostburg A * </p> -  *  -  * @author: Eric.chen the * @date: September 23, 2017 PM 12:54:05 -  */ -  Public classDoublelinkedlist { -     classNode {//node Element +         PrivateNode prev; -          PublicObject value; +         PrivateNode Next; A     } at  -     PrivateNode Header =NULL; -  -     voidInitiate ()//Initialize the list of links -     { -Header =NewNode (); inHeader.prev =header; -Header.value =NULL; toHeader.next =header; +     } -  the      Public voidInsertlist (intI, Object N)//insert element N in the I position of the list *     { $         if(I <= 0 | | i >size ()) {Panax NotoginsengSYSTEM.OUT.PRINTLN ("The insertion position is NOT legal!!! The "+" linked list length is: "+size ()); -}Else { theNode e =NewNode (); +E.value =N; A             if(Header.prev = = header)//This is the first time the element is inserted, and the element is inserted after the head node. the             { +E.prev =header; -E.next =header; $Header.next =e; $Header.prev =e; -  -}Else if(i = = size ())//insert element at last the             { -E.next =header;WuyiE.prev =Header.prev; theHeader.prev.next =e; -Header.prev =e; Wu}Else{//inserting elements at position I -Node temp =header; About                 intCount = 0; $                  while(Temp.next! =header) { -count++; -                     if(Count = =i) { -E.next =Temp.next; AE.prev =temp; +Temp.next.prev =e; theTemp.next =e; -  $                     } thetemp =Temp.next; the                 } the  the             } -         } in     } the  the      Public voidDeletebyindex (intI//Delete an element at a specified location About     { the         if(I <= 0 | | i >size ()) { theSystem.out.println ("Deleted position error" + "linked list length:" +size ()); the}Else { +Node temp =header; -             intCount = 0; the              while(Temp.next! =header) {Bayicount++; the                 if(Count = =i) { theTemp.next =Temp.next.next; -Temp.next.prev =temp; -  the                 } the  thetemp =Temp.next; the             } -  the         } the  the     }94  the      Public intSize ()//returns the length of the linked list the     { the         intCount = 1;98Node temp =header; About          while(Temp.next! =header) { -count++;101temp =Temp.next;102         }103 104         returncount; the     }106 107      Public voidprintlist () {108Node temp =header;109          while(Temp.next! =header) { theSystem.out.print (Temp.next.value + "");111temp =Temp.next; the         }113     } the  the      Public voidDeletebyele (Object Element)//To delete a specified element in a linked list the     {117Node temp =header;118         intCount = 0;119         intnum = 0; -          while(Temp.next! =header) {121count++;122             if(Temp.next.value = =Element) {123num =count;124             } thetemp =Temp.next;126         }127 deletebyindex (num); -     }129  the      PublicObject Findelebyindex (inti) {131         intCount = 0; theObject p =NULL;133         if(I <= 0 | | i >size ()) {134SYSTEM.OUT.PRINTLN ("The input data is wrong!") " +size ());135             return-1;136}Else {137Node temp =header;138              while(Temp.next! =header) {139count++; $                 if(Count = =i) {141p =Temp.next.value;142                 }143temp =Temp.next;144             }145         }146         returnp;147     }148 149      Public Static voidMain (string[] args) { MaxDoublelinkedlist list =Newdoublelinkedlist ();151 list.initiate (); the          for(inti = 1; I <= 10; i++) {153 list.insertlist (i, i);154         }155System.out.println ("The original linked list is:");156 list.printlist ();157 System.out.println ();158System.out.println ("The length of the linked list is:" +list.size ());159         /*List.deletebyele (8); the System.out.println ("The linked list after deleting the element is:");161 list.printlist ();162 System.out.println ();163 System.out.println ("The length of the linked list after deleting the element is:" + list.size ());*/164         intidem = (int) List.findelebyindex (8);165 System.out.println (idem);166 167     }168}

Java implements the basic operation of the bidirectional circular list

Related Article

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.