Estructuras
CHAPTER 5
Linked Lists
current.previous.next = current.next; if(current==last) last = current.previous; else // last item? // old previous <-- last // not last // old previous <-- old nextcurrent.next.previous = current.previous;
The doublyLinked.java Program
Listing 5.8 shows the complete doublyLinked.java program, which includes all the routines just discussed. LISTING 5.8 ThedoublyLinked.java Program
// doublyLinked.java // demonstrates doubly-linked list // to run this program: C>java DoublyLinkedApp ////////////////////////////////////////////////////////////////class Link { public long dData; // data item public Link next; // next link in list public Link previous; // previous link in list //------------------------------------------------------------public Link(long d) // constructor { dData = d; } // ------------------------------------------------------------public void displayLink() // display this link { System.out.print(dData + “ “); } //------------------------------------------------------------} // end class Link //////////////////////////////////////////////////////////////// class DoublyLinkedList { private Link first; // refto first item private Link last; // ref to last item // ------------------------------------------------------------public DoublyLinkedList() // constructor { first = null; // no items on list yetlast = null; }
Doubly Linked Lists
227
LISTING 5.8
Continued
// ------------------------------------------------------------public boolean isEmpty() // true if no links { returnfirst==null; } // ------------------------------------------------------------public void insertFirst(long dd) // insert at front of list { Link newLink = new Link(dd); // make new link if( isEmpty() ) // ifempty list, last = newLink; // newLink <-- last else first.previous = newLink; // newLink <-- old first newLink.next = first; // newLink --> old first first = newLink; // first --> newLink } //...
Regístrate para leer el documento completo.