12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 4.1 Lists 111// Linked list implementationclass LList implements List {private Link head;// Pointer <strong>to</strong> list headerprivate Link tail;// Pointer <strong>to</strong> last elementprotected Link curr; // Access <strong>to</strong> current elementint cnt;// Size of list//Construc<strong>to</strong>rsLList(int size) { this(); } // Construc<strong>to</strong>r -- Ignore sizeLList() {curr = tail = head = new Link(null); // Create headercnt = 0;}public void clear() {// Remove all elementshead.setNext(null);// Drop access <strong>to</strong> linkscurr = tail = head = new Link(null); // Create headercnt = 0;}// Insert "it" at current positionpublic void insert(E it) {curr.setNext(new Link(it, curr.next()));if (tail == curr) tail = curr.next(); // New tailcnt++;}public void append(E it) { // Append "it" <strong>to</strong> listtail = tail.setNext(new Link(it, null));cnt++;}// Remove <strong>and</strong> return current elementpublic E remove() {if (curr.next() == null) return null; // Nothing <strong>to</strong> removeE it = curr.next().element();// Remember valueif (tail == curr.next()) tail = curr; // Removed lastcurr.setNext(curr.next().next()); // Remove from listcnt--;// Decrement countreturn it;// Return value}Figure 4.8 A linked list implementation.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!