Double-stranded collection Add Delete algorithm

Source: Internet
Author: User

To add a delete algorithm to a double-stranded collection:
Package com.linkes;PublicClassmylinkelist {/** * More information Welcome to the Kay Academy website: http://kaige123.com *@author Small Foam * */** * 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. */Private object[] Shou;Private object[] Wei;Default Add MethodPublicvoidAdd(Object obj) {if (Shou = =NULL) {The first null is the new good space 1th and the third place null the second put the data passed in Shou =New object[] {NULL, obj,Null};wei = Shou;Point the tail to the header}else {If the header has data, then the new 3-length spacePut Wei into the first point to the home, the second into the current data, the third is nullobject[] Objs =New object[] {wei, obj,Null};wei[2] = OBJS;The third of the tail points to Objswei = OBJS;OBJS updated to Tail}}Add a method from the backPublicvoidAddLast(Object obj) {Add (obj);}Add a method from the frontPublicvoidAddFirst(Object obj) {if (Shou = =NULL) {Shou =New object[] {NULL, obj,Null};wei = Shou;}else {object[] Objs =New object[] {NULL, obj, Shou};shou[0] = Objs;shou = Objs;}}Delete all methodsPublicvoidRemoveAll(int Delall) {if (Delall = =0) {Wei =NullThe direct production of empty is all deleted Shou =Null System.out.println ("Clear Complete");}else {try {ThrowNew Exception ("Delete failed!" Delall need to be ' 0 ' ");}catch (Exception e) {e.printstacktrace ();}}}Remove a method from the backPublic ObjectRemovelast() {Object obj =Nullif (Wei = =NULL) {No tail so directly returns falseReturnfalse;}Do you have a homeif (wei[0] = =NULL) {If there is no direct tail with the first system empty because no home means there is only one data Wei =Null;shou =null;}else {If you have a home then give the data in the tail 1 to Objobj = wei[1];wei = (object[]) wei[0];The tail points to the trailing subscript 0wei[2] =null;}return obj;}Remove a method from the frontPublic ObjectRemovefirst() {Object obj =Null;obj = shou[1];if (Shou = =NULL) {Returnfalse;}if (shou[2] = =NULL) {Wei =Null;shou =null;}else {Shou = (object[]) shou[2];shou[0] =null;}return obj;}Remove a method from the middlePublicBooleanRemove(Object obj) {object[] objs = Shou;while (True) {Look up and down homeData passed in is not equal to OBJS current dataif (Obj.equals (objs[1])) {BreakIf that's the case, it's the bounce loop.If the data passed in is not the current data then list OBJS = (object[]) objs[2];if (Objs = =NULL) {If there's no more, then jump out of the loop.Break;}}if (Objs = =NULL) {Returnfalse;}if (Objs = = Shou) {Ask whether to delete the header Removefirst ();}Elseif (Objs = = Wei) {is not to delete tail removelast ();}else {Split the current data into home and home object[] Shang = (object[]) objs[0];object[] Xia = (object[]) objs[2];shang[2] = Xia;The home of the subscript 2 cited the next data xia[0] = Shang;The next subscript 0 quoted the home data}Returntrue;}Private object[] dq =NullQuery data retrieval with HashnextPublicBooleanHashnext() {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() {return dq[1];}}       
Test class:
Ackage Com.linkes; public class test {public static void main new mylinkelist (); Linke.add ( "a"); Linke.addlast ( "B"); Linke.addfirst ( "F"); Linke.addfirst ( 1);                

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.