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.

Sec. 4.1 Lists 109/** Singly linked list node with freelist support */class Link {priv<strong>at</strong>e E element; // Value for this nodepriv<strong>at</strong>e Link next; // Point to next node in list/** Constructors */Link(E it, Link nextval){ element = it; next = nextval; }Link(Link nextval) { next = nextval; }/** Get <strong>and</strong> set methods */Link next() { return next; }Link setNext(Link nxtval) { return next = nxtval; }E element() { return element; }E setElement(E it) { return element = it; }/** Extensions to support freelists */st<strong>at</strong>ic Link freelist = null; // Freelist for the class/** @return A new link */st<strong>at</strong>ic Link get(E it, Link nextval) {if (freelist == null)return new Link(it, nextval); // Get from "new"Link temp = freelist;// Get from freelistfreelist = freelist.next();temp.setElement(it);temp.setNext(nextval);return temp;}}/** Return a link to the freelist */void release() {element = null; // Drop reference to the elementnext = freelist;freelist = this;}Figure 4.11 Implement<strong>at</strong>ion for the Link class with a freelist. The st<strong>at</strong>icdeclar<strong>at</strong>ion for member freelist means th<strong>at</strong> all Link class objects share thesame freelist pointer variable instead of each object storing its own copy.

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

Saved successfully!

Ooh no, something went wrong!