Nothing idle: Implementation of Java double-stranded tables:
Package org.com. data_structure.list.demo; </P> <p> Import Java. util. list; </P> <p>/** <br/> * dual-chain table implementation <br/> * @ author NMW <br/> */<br /> Public final class doublelinked {</P> <p>/** <br/> * Header node <br/> */<br/> private transient node head = new node (null, null, null); </P> <p>/** <br/> * indicates that elements can be inserted at the current position to indicate the size of the linked list. <br/> */<br/> private transient int size = 0; </P> <p>/** <br/> * Create a linked list without elements <br/> */<br/> Pu BLIC doublelinked () {<br/> head. next = head. previous = head; <br/>}</P> <p>/** <br/> * Create a New linked list based on the given linked list/<br/> * @ Param d <br/> */<br/> Public doublelinked (doublelinked D) {<br/> this (); <br/> This. addall (d); <br/>}</P> <p>/** <br/> * size of the linked list. <br/> * @ return <br/> */<br/> Public int size () {</P> <p> return size; <br/>}</P> <p>/** <br/> * Whether the linked list is empty. <br/> * @ return true or false <br/> */<br/> Public boolea N isempty () {</P> <p> return size = 0; <br/>}</P> <p>/** <br/> * whether to include a given element <br/> * @ Param OBJ <br/> * @ return true or false <br/> */<br/> Public Boolean containts (Object OBJ) {</P> <p> return this. indexof (OBJ )! =-1; <br/>}</P> <p>/** <br/> * remove the first element of the linked list. If the linked list is empty, null is returned. <br/> * @ return return the deleted element <br/> */<br/> Public object remove () {</P> <p> Object OBJ = NULL; <br/> If (this. size = 0) <br/> return NULL; </P> <p> node P = head. next; // obtain the first element </P> <p> P. next. previous = head; // restructured the linked list <br/> head. next = P. next; </P> <p> This. size --; // linked list size-1 <br/> OBJ = P. OBJ; <br/> return OBJ; <br/>}</P> <p>/** <br/> * remove the last element from the linked list. If the linked list is empty, A Nu is returned. Ll <br/> * @ return returns the last element <br/> */<br/> Public object removelast () {</P> <p> Object OBJ = NULL; <br/> If (this. size = 0) <br/> return NULL; </P> <p> node P = this. head. previous; // obtain the last node <br/> // reconstruct the linked list <br/> P. previous. next = head; // The next element of the last element of P points to head <br/> head. previous = P. previous; // The previous element of the head points to the previous element of P/</P> <p> This. size --; <br/> OBJ = P. OBJ; <br/> return OBJ; <br/>}</P> <p>/** <br/> * remove the element at the specified index if ind Ex <0 or index> size indicates a bubble exception <br/> * The element subscript starts from 0 <br/> * @ Param index <br/> * @ return returns element/<br/> */<br/> Public object remove (INT index) throws indexoutofboundsexception {</P> <p> This. check (INDEX); // check whether the specified index is reasonable </P> <p> node = This. getnode (INDEX); </P> <p> node P = node. previous; // The previous element of the current element <br/> node n = P. next; // The next element of the current element <br/> P. next = N; <br/> N. previous = P; </P> <p> This. size --; <br/> return node. OBJ; <br/>}</P> <p>/** <br/> * remove the specified element when the first occurrence of the linked list <br/> * @ Param OBJ <br/> * @ return returns true if yes; otherwise, returns false. <br/> */<br/> Public Boolean remove (Object OBJ) {</P> <p> If (this. size = 0) <br/> return false; </P> <p> int I = 0; <br/> node = This. head; <br/> Boolean fog = false; <br/> int size = This. size; <br/> If (OBJ = NULL) <br/> while (I <size) {<br/> node = node. next; <br/> If (node. OBJ = NULL) {<br/> Fog = tru E; <br/> break; // indicates that the node is found <br/>}< br/> I ++; <br/>}< br/> else {<br/> while (I <size) {<br/> node = node. next; <br/> If (obj. equals (node. OBJ) {<br/> Fog = true; <br/> break; // indicates that the node is found <br/>}< br/> I ++; <br/>}< br/> // restructured the linked list <br/> If (FOG) {<br/> node P = node. previous; <br/> node n = node. next; <br/> P. next = N; <br/> N. previous = P; <br/>}</P> <p> return fog; <br/>}</P> <p>/** <br/> * Delete the last occurrence node of an element. <br/> * @ Param o <br/> * @ return <br/> */<br/> Public Boolean removelastoccurrence (Object OBJ) {</P> <p> If (this. size = 0) <br/> return false; <br/> Boolean fog = false; <br/> int I = 0; <br/> int S = This. size; <br/> node = This. head; <br/> node lastnode = NULL; // The Last node that appears <br/> If (OBJ = NULL) {<br/> while (I <s) {<br/> node = node. next; <br/> If (node. OBJ = NULL) {<br/> lastnode = node; <br/> Fog = true; // locate the last occurrence <Br/>}< br/> I ++; <br/>}< br/> else {<br/> while (I <s) {<br/> node = node. next; <br/> If (obj. equals (node. OBJ) {<br/> lastnode = node; <br/> Fog = true; <br/>}< br/> I ++; <br/>}</P> <p> // restructured the linked list <br/> If (FOG) {<br/> node P = lastnode. previous; <br/> node n = lastnode. next; <br/> P. next = N; <br/> N. previous = P; <br/>}</P> <p> return fog; <br/>}</P> <p>/** <br/> * check whether the specified value is out of bounds. <br/> * @ Param index <br/> * @ Throws indexoutofboundsexception <br/> */<br/> private void check (INT index) throws indexoutofboundsexception {</P> <p> If (index <0 | index> = This. size) <br/> throw new indexoutofboundsexception ("the input value is invalid:" + index ); <br/>}</P> <p>/** <br/> * append an element to the end of the linked list/<br/> * @ Param OBJ <br/> * @ return <br/> */<br/> Public Boolean add (Object OBJ) {</P> <p> node = This. createnode (OBJ); <br/> node P = This. head. pre Vious; // locate the last element </P> <p> node. previous = P; // set the previous element of the newly added element <br/> node. next = head; // set the next element of the new element </P> <p> P. next = node; // set to add the next element of the last element./<br/> This. head. previous = node; <br/> This. size ++; // linked list size + 1 <br/> return true; <br/>}</P> <p>/** <br/> * append the specified element to the specified position. <br/> * @ Param index <br/> * @ Param OBJ <br/> * @ return <br/> */<br/> Public void add (INT index, object OBJ) {</P> <p> This. check (INDEX); </P> <p> N Ode node = This. createnode (OBJ); // create a new node </P> <p> node indexnode = This. getnode (INDEX); // obtain the node at the current index </P> <p> node P = indexnode. previous; // The previous node of the current index </P> <p> node. previous = P; // the last element of the new node <br/> node. next = indexnode; // The next element of the new node </P> <p> P. next = node; // The next node of the current node points to the new node </P> <p> indexnode. previous = node; // The previous element of the current node points to the new node </P> <p> This. size ++; <br/>}</P> <p>/** <br/> * Add a node to the first node <br/> * @ Param OBJ <br/> * @ return true <br/> */<br/> Public Boolean addfirst (Object OBJ) {</P> <p> node newnode = This. createnode (OBJ); // create a node <br/> node n = head. next; </P> <p> newnode. next = N; <br/> newnode. previous = head; </P> <p> head. next = newnode; <br/> This. size ++; <br/> return true; <br/>}</P> <p>/** <br/> * obtain the first element <br/> * @ return <br/> */<br/> Public object getfirst () {</P> <p> If (this. size = 0) {<br/> return n Ull; <br/>}< br/> return this. head. next. OBJ; <br/>}</P> <p>/** <br/> * obtain the last element, if the linked list is empty, null is returned. Otherwise, the value of the last node is returned. <br/> * @ return <br/> */<br/> Public object getlast () {</P> <p> If (this. size = 0) {<br/> return NULL; <br/>}< br/> return this. head. previous. OBJ; <br/>}</P> <p>/** <br/> * The subscript of the node value at the specified index starts from 0. <br/> * @ Param index <br/> * @ return <br/> */<br/> Public object get (INT index) {</P> <p> This. check (INDEX); </P> <P> Object OBJ = NULL; <br/> node = This. getnode (INDEX); <br/> OBJ = node. OBJ; <br/> return OBJ; <br/>}</P> <p>/** <br/> * index starts from 0 <br/> * @ Param OBJ <br/> * @ return if the given element is indexed in the linked list, null is returned. <br/> */<br/> Public int indexof (Object OBJ) {</P> <p> int I = 0; <br/> node = This. head; <br/> int S = This. size; <br/> If (OBJ = NULL) {<br/> while (I <s) {<br/> node = node. next; <br/> If (node = NULL) <br/> return I; <Br/> I ++; <br/>}< br/>}else {<br/> while (I <s) {<br/> node = node. next; <br/> If (obj. equals (node. OBJ) <br/> return I; <br/> I ++; <br/>}</P> <p> return-1; <br/>}</P> <p>/** <br/> * clear the linked list <br/> */<br/> Public void clear () {<br/> This. head. next = This. head. previous = NULL; <br/> This. size = 0; <br/>}</P> <p>/** <br/> * replace the node at the specified index <br/> * @ Param index <br/> *@ param OBJ <br/> * @ return <br/> */<br/> Pu BLIC object set (INT index, object OBJ) {</P> <p> This. check (INDEX); </P> <p> node = This. getnode (INDEX); // get the node at the specified index <br/> Object o = node. OBJ; // obtain the node value <br/> node. OBJ = OBJ; // Replace the node value </P> <p> return O; <br/>}</P> <p>/** <br/> * returns the last index value of the element. <br/> * returns if the element is included. the subscript returns-1 <br/> * @ Param OBJ <br/> * @ return <br/> */<br/> Public int lastindexof (Object OBJ) {<br/> int Index =-1; <br/> int I = 0; <br/> int S = This. size; <br/> node = This. head; <br/> If (OBJ = NULL) {<br/> while (I <s) {<br/> node = node. next; <br/> If (node. OBJ = NULL) {<br/> Index = I; <br/>}< br/> I ++; <br/>}< br/> else {<br/> while (I <s) {<br/> node = node. next; <br/> If (obj. equals (node. OBJ) {<br/> Index = I; <br/>}< br/> I ++; <br/>}</P> <p> Return Index; <br/>}</P> <p>/** <br/> * returns the array representation of the linked list. <br/> * @ return <br/> */< br/> publi C object [] toarray () {</P> <p> int objlength = This. size; <br/> object [] OBJ = new object [objlength]; <br/> node = This. head; <br/> int I = 0; <br/> while (I <objlength) {<br/> node = node. next; <br/> OBJ [I ++] = node. OBJ; <br/>}</P> <p> return OBJ; <br/>}</P> <p>/** <br/> * append the specified linked list to the current linked list. <br/> * @ Param d <br /> * @ return <br/> */<br/> Public Boolean addall (doublelinked D) {<br/> If (D = NULL) <br/> throw new Nullpointerexception ("the linked list cannot be blank +" + d); <br/> node tlast = This. head. previous; // The previous node of the current node <br/> node dfirst = D. head. next; // specify the first node of the linked list <br/> node dlast = D. head. previous; // specify the last node of the node </P> <p> tlast. next = dfirst; // point the last node of the linked list to the first node of the given linked list. <br/> dfirst. previous = tlast; // specify the previous node of the first element of the linked list pointing to the last node of the linked list </P> <p> dlast. next = This. head; <br/> This. head. previous = dlast; <br/> This. size = This. size + D. size; // The length of the new linked list Degree <br/> return true; <br/>}</P> <p>/** <br/> * construct a node <br/> * @ Param OBJ <br/> * @ return node <br/> */<br/> private node createnode (Object OBJ) {<br/> return new node (OBJ, null, null); <br/>}</P> <p> private node getnode (INT index) {<br/> check (INDEX); <br/> node = This. head; <br/> int fog = This. size-index; <br/> int I = 0; <br/> If (fog <= index) {<br/> while (I <index) {<br/> node = node. next; <br/> I ++; <br/ >}< Br/> else {<br/> while (I <fog) {<br/> node = node. previous; <br/> I ++; <br/>}< br/> return node; <br/>}</P> <p>/** <br/> * node element <br/> * @ author NMW <br/> * /<br/> Private Static class node {<br/> node next; // The next element of the node <br/> Object OBJ; // node value <br/> node previous; // an original element on the node </P> <p> Public node (Object OBJ, node next, node previous) {<br/> This. OBJ = OBJ; <br/> This. next = next; <br/> T His. previous = previous; <br/>}</P> <p>/** <br/> * checks whether two linked lists are equal. If each element of the linked list is equal, true is returned. <br/> */<br/> @ override <br/> Public Boolean equals (Object OBJ) {<br/> If (OBJ = This) <br/> return true; <br/> else if (OBJ instanceof doublelinked) {<br/> int I = 0; <br/> node n = This. head; <br/> doublelinked d = (doublelinked) OBJ; <br/> node o = D. head; <br/> If (this. size! = D. size) <br/> return false; <br/> while (I <this. size) {<br/> N = n. next; <br/> O = O. next; <br/> If (N. OBJ = NULL) {<br/> If (O. OBJ! = NULL) <br/> return false; <br/>}< br/> else {<br/> If (! N. OBJ. equals (O. OBJ) {<br/> return false; <br/>}< br/> return true; <br/>}</P> <p> return false; <br/>}</P> <p>/** <br/> * returns the string representation of the linked list. <br/> */<br/> @ override <br/> Public String tostring () {<br/> stringbuffer S = new stringbuffer ("["); <br/> node = This. head; <br/> int I = 0; <br/> while (I <this. size) {<br/> node = node. next; <br/> S. append (node. OBJ); <br/> I ++; <br/> if (I! = This. size) {<br/> S. append (","); <br/>}< br/> S. append ("]"); </P> <p> return S. tostring (); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> doublelinked d = new doublelinked (); <br/> for (INT I = 0; I <10; I ++) {<br/> D. add (I); <br/>}< br/> doublelinked DL = new doublelinked (d); <br/> system. out. println (DL. tostring (); <br/>}</P> <p >}< br/>