11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

104 Chap. 4 Lists, Stacks, <strong>and</strong> Queues/** Linked list implement<strong>at</strong>ion */class LList implements List {priv<strong>at</strong>e Link head;// Pointer to list headerpriv<strong>at</strong>e Link tail;// Pointer to last elementprotected Link curr; // Access to current elementint cnt;// Size of list/** Constructors */LList(int size) { this(); } // Constructor -- Ignore sizeLList() {curr = tail = head = new Link(null); // Cre<strong>at</strong>e headercnt = 0;}/** Remove all elements */public void clear() {head.setNext(null);// Drop access to linkscurr = tail = head = new Link(null); // Cre<strong>at</strong>e headercnt = 0;}/** Insert "it" <strong>at</strong> current position */public void insert(E it) {curr.setNext(new Link(it, curr.next()));if (tail == curr) tail = curr.next(); // New tailcnt++;}/** Append "it" to list */public void append(E it) {tail = tail.setNext(new Link(it, null));cnt++;}/** Remove <strong>and</strong> return current element */public E remove() {if (curr.next() == null) return null; // Nothing to 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}/** Set curr <strong>at</strong> list start */public void moveToStart(){ curr = head; }Figure 4.8 A linked list implement<strong>at</strong>ion.

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

Saved successfully!

Ooh no, something went wrong!