Implementation of Java double-stranded tables

Source: Internet
Author: User
/*****/Package COM. handy. DS;/*** @ author handy **/public class doublelinkedlist {class node {node Prev; node next; object data; Public node (Object Data, node Prev, node next) {This. data = data; this. prev = Prev; this. next = next;} public node (node Prev, node next) {This. prev = Prev; this. next = next;} public node () {}} private node head; private int size;/*** @ return the size */Public int getsize () {retu Rn size;}/*** @ Param size * the size to set */Public void setsize (INT size) {This. size = size;} private node getnode (INT index) {checkindex (INDEX); node curr = This. head; int revindex = This. size-index; If (index <= revindex) {for (INT I = 0; I <= index; I ++) curr = curr. next;} else {for (INT I = 0; I <revindex; I ++) curr = curr. prev;} return curr;} public doublelinkedlist () {// first element storage value head = new node (); // The header pointer does not store a value. You can initialize the head in this way. prev = head. next = head;} public Boolean isempty () {return size = 0;} public Boolean contains (Object O) {node curr = This. head. next; node last = head. prev; while (curr! = NULL | last! = NULL) {If (curr. data. equals (O) return true; If (last. data. equals (O) return true; node temp = last; curr = curr. next; last = last. prev; If (curr. next = temp) break; // pointer touch} return false; // or use the following simpler method // If (indexof (o )! =-1) // return true; // return false;} // remove the first element of the linked list. Because the head does not save information, the first element is head. next, so move the head. nextpublic object removefirst () {If (this. size = 0) return NULL; else {node first = head. next; first. next. prev = head; head. next = first. next; this. size --; return first ;}}// remove the last public object removelast () {If (this. size = 0) return NULL; else {node last = head. prev; node lastsecond = last. prev; lastsecond. next = head; Head. prev = lastsecond; this. size --; return last ;}/// remove the I-th element public object remove (INT index) throws indexoutofboundsexception {checkindex (INDEX); int size = This. size; node curr = getnode (INDEX); node P = curr. prev; node n = curr. next; p. next = N; N. prev = P; this. size --; return curr;} // remove the first appeared O element public object removefist (Object O) {If (this. size = 0) return NULL; else {node curr = This. head. next; Boolean is Find = false; int size = This. size; int I = 0; while (I <size) {If (curr. data. equals (O) {isfind = true; break;} curr = curr. next; I ++;} If (isfind) {// reconstructs the linked list node P = curr. prev; node n = curr. next; p. next = N; N. prev = P; this. size --; return curr;} return NULL ;}// remove the last appearing O element public object removelastoccurrence (Object O) {If (this. size = 0) return NULL; else {node curr = This. head. next; node lastoccurrenceno De = NULL; int I = 0; int size = This. size; while (I <size) {If (curr. data. equals (O) lastoccurrencenode = curr; curr = curr. next; I ++;} If (lastoccurrencenode! = NULL) {// find the last element and refactor the linked list node P = lastoccurrencenode. prev; node n = lastoccurrencenode. next; p. next = N; N. prev = P; this. size --; return lastoccurrencenode;} return NULL ;}// check whether the public Boolean checkindex (INT index) throws indexoutofboundsexception {If (index <0 | index> = This. size) {Throw new indexoutofboundsexception ("the boundary is invalid:" + index);} return true ;}// add public Boolean addlast (Object O) {Node Last = head. prev; node lastsecond = last. prev; node newnode = new node (O, last, head); last. next = newnode; head. prev = newnode; this. size ++; return true;} // public Boolean add (INT index, object O) {node curr = getnode (INDEX); node P = curr. prev; // node n = curr. next; node newnode = new node (O, P, curr); curr. prev = newnode; p. next = newnode; this. size ++; return true;} // Add public Boolean addfirs to the front T (Object O) {node curr = head. next; node newnode = new node (O, Head, curr); head. next = newnode; curr. prev = newnode; this. size ++; return true ;}// obtain the first public object getfist () {return head. next;} // get the last element public object getlast () {return head. prev;} // obtain the value of the element at the specified position. Public object get (INT index) {checkindex (INDEX); node curr = getnode (INDEX); return curr. data;} // return the index of the position of the element in the Linked List (starting from 0). If the element is not in the linked list,-1 Public int is returned. Indexof (Object O) {int Index = 0; int size = This. size; node curr = head. next; while (index <size) {If (curr. data. equals (O) Return Index; curr = curr. next; index ++;} return-1;} // Replace the public Boolean set (INT index, object O) {node curr = getnode (INDEX) at the specified index ); curr. data = O; return true;} // returns the last index value of the element. If the element does not exist,-1 Public int lastindexof (Object O) {int Index =-1; node curr = head. next; node = NULL; int Size = This. size; int I = 0; // The loop variable while (I <size) {If (curr. data. equals (O) {node = curr; Index = I;} curr = curr. next; I ++;} Return Index;} // returns the array of the linked list to indicate public object [] toarray () {object [] array = new object [this. size]; node curr = head. next; For (INT I = 0; I <size; I ++, curr = curr. next) {array [I] = curr. data;} return array;} // append the specified linked list to the public Boolean addall (doublelinkedlist DLL) {If (DLL = NUL L) throw new nullpointerexception ("the linked list cannot be blank! "+ DLL); node last = head. prev; node dlllast = DLL. head. prev; dlllast. next = head; DLL. head. prev = last; head. prev = dlllast; last. next = DLL. head. next; this. size = This. size + DLL. size; return true;} public void clear () {This. head. next = head. prev = NULL; this. size = 0 ;}// compare whether two linked lists are equal public Boolean equals (Object O) {If (O = This) return true; else {If (O instanceof doublelinkedlist) {int size = This. size; Dou Blelinkedlist DLL = (doublelinkedlist) O; If (size! = DLL. size) return false; else {int I; node p, q; For (P = This. head. next, q = DLL. head. next, I = 0; I <size; I ++, P = P. next, q = Q. next) {If (P. data = NULL & Q. data! = NULL) return false; else // is this recursion? Or call the equals method of the object class? If (! P. data. equals (Q. data) return false;} return true;} elsereturn false;} // return the string of the linked list to indicate Public String tostring () {stringbuffer sb = new stringbuffer ("["); node curr = head. next; For (INT I = 0; I <this. size; I ++, curr = curr. next) {if (I = size-1) Sb. append (curr. data); elsesb. append (curr. data + ",");} sb. append ("]"); return new string (SB);} public static void main (string [] ARGs) {doublelinkedlist DLL = new Doublelinkedlist (); DLL. addfirst (5); system. out. println (DLL. tostring (); // [5] DLL. removefirst (); system. out. println (DLL. isempty (); // truedll. removefirst (); system. out. println (DLL. tostring (); // [] DLL. removefist (5); system. out. println (DLL. tostring (); // [] DLL. removelast (); system. out. println (DLL. tostring (); // [] DLL. removelastoccurrence (5); system. out. println (DLL. tostring (); // [] DLL. addlast (10); system. Out. println (DLL. tostring (); // [10] DLL. addfirst (30); system. out. println (DLL. tostring (); // [30, 10] system. out. println (DLL. get (0); system. out. println (DLL. get (1); DLL. addfirst (40); DLL. addlast (20); DLL. addlast (50); system. out. println (DLL. tostring (); // [40, 30, 10, 20, 50] system. out. println (DLL. get (4); system. out. println (DLL. get (3); DLL. add (3,100); system. out. println (DLL. tostring (); // [40, 30, 10,100, 20, 50] DL L. removefirst (); system. out. println (DLL. tostring (); // [30,10, 100,20, 50] DLL. removelast (); system. out. println (DLL. tostring (); // [30,10, 100,20] DLL. addfirst (30); DLL. addfirst (40); DLL. addfirst (30); system. out. println (DLL. tostring (); // [10,100, 20] system. out. println (DLL. getsize (); // [10,100, 20] DLL. removefist (30); system. out. println (DLL. tostring (); // [, 30,] DLL. addlast (10); system. out. println (DLL. tostring (); // [, 30, 10] DLL. removelastoccurrence (10); system. out. println (DLL. tostring (); // [, 30,] system. out. println (node) DLL. getfist ()). data); // 40system. out. println (DLL. get (3); // 10system. out. println (node) DLL. getlast ()). data); // 10dll. set (1, 70); system. out. println (DLL. tostring (); // [,] DLL. addfirst (10); system. out. prin TLN (DLL. tostring (); // [10,100, 20] system. out. println (DLL. indexof (10); // 0system. out. println (DLL. lastindexof (10); // 3system. out. println (DLL. contains (100); // truesystem. out. println (DLL. contains (50); // truedoublelinkedlist dll2 = new doublelinkedlist (); dll2.addlast (200); dll2.addfirst (400); system. out. println (dll2.tostring (); // [400,200] DLL. addall (dll2); system. out. println (DLL. tostring (); // [400,200,] object [] O = DLL. toarray (); // [400,200,] system. out. print ("["); For (INT I = 0; I <O. length; I ++) {if (I! = O. length-1) system. out. print (O [I] + ","); elsesystem. out. print (O [I]);} system. out. println ("]") ;}}

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.