Double-stranded collection Add Delete algorithm

Source: Internet
Author: User

To add a delete algorithm to a double-stranded collection:
 PackageCom.linkes; Public  class mylinkelist {/** * More information Welcome to the Kay Academy website: http://kaige123.com * @author Small Mo * * /** * List of linked lists He is in the double chain to carry out the reference to the home, the benefits can be known to the house and the next who * conducive to modification, can be deleted from the beginning of the data can also be deleted from the tail. * Can be removed from the middle specified location. */ PrivateObject[] Shou;PrivateObject[] Wei;//Default Add Method Public   void add(Object obj) {if(Shou = =NULL) {//Header null on new good space 1th and third put null a second put in the data passed inShou =NewObject[] {NULLObjNULL};wei = Shou;//Put the tail point to the header}Else{//If the header has data, then the new good length of 3 space //Put Wei into the first point to the home, the second puts the current data, the third is nullobject[] Objs =NewObject[] {wei, obj,NULL};wei[2] = OBJS;//The third point of the tail points to the ObjsWei = objs;//objs updated to tail}}//Add the method from the back Public   void addlast(Object obj) {Add (obj);}//Add the method from the front Public   void addfirst(Object obj) {if(Shou = =NULL) {Shou =NewObject[] {NULLObjNULL};wei = Shou;}Else{object[] Objs =NewObject[] {NULL, obj, Shou};shou[0] = Objs;shou = Objs;}}//Delete all methods Public   void removeall(int delall) {if(Delall = =0) {Wei =NULL;//Direct making empty all deletedShou =NULL; System.out.println ("Clear Complete");}Else{Try{Throw NewException ("Delete failed!" Delall needs to be ' 0 ' ");}Catch(Exception e) {E.printstacktrace ();}}}//Remove the method from the back Public   Object removelast() {Object obj =NULL;if(Wei = =NULL) {//No tail so direct return false return false;}// Do you have a home if(wei[0] ==NULL) {//If there is no direct tail-to-head system because no home means there is only one dataWei =NULL; shou =NULL;}Else{//Have a home then give the data in the tail 1 to objobj = wei[1];wei = (object[]) wei[0];//tail pointing to the rear subscript 0wei[2] =NULL;}returnobj;}//Remove the method from the front Public   Object removefirst() {Object obj =NULL; obj = shou[1];if(Shou = =NULL) {return false;}if(shou[2] ==NULL) {Wei =NULL; shou =NULL;}Else{Shou = (object[]) shou[2];shou[0] =NULL;}returnobj;}//Remove the method from the middle Public   boolean remove(Object obj) {object[] objs = Shou; while(true) {//Look up and down home //Data passed in is not equal to OBJS current data if(Obj.equals (objs[1])) { Break;//If that's the case, it's a bounce-out loop .}//If the data passed in is not current data then list theOBJS = (object[]) objs[2];if(Objs = =NULL) {//If there is no more, then jump out of the loop  Break;}}if(Objs = =NULL) {return false;}if(Objs = = Shou) {//Ask whether to delete headerRemovefirst ();}Else if(Objs = = Wei) {//Is not deleted tailRemovelast ();}Else{//Split the current data into home and homeobject[] Shang = (object[]) objs[0];object[] Xia = (object[]) objs[2];shang[2] = Xia;//The home of the subscript 2 cited the home dataxia[0] = Shang;//The next subscript 0 of the home cited the home data}return true;}PrivateObject[] dq =NULL;//Use Hashnext to ask for data retrieval Public   boolean hashnext() {if(dq = =NULL) {dq = shou;if(dq = =NULL) {return false;}return true;} dq = (object[]) dq[2];if(dq = =NULL) {return false;}return true;}//Use next to get the value Public   Object Next() {returndq[1];}}
Test class:
Ackage Com.linkes; Public  class Test {Public   static void main(string[] args) {Mylinkelist Linke =NewMylinkelist (); Linke.add ("a"); Linke.addlast ("B"); Linke.addfirst ("C"); Linke.addfirst ("D"); Linke.addfirst ("E"); Linke.addfirst ("F"); Linke.addfirst ("G"); Linke.removeall (1); while(Linke.hashnext ()) {System.out.println (linke). Next ());}}}

Double-stranded collection Add Delete algorithm

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.