/** Source code example for "A Practical Introduction to Data Structures and Algorithm Analysis, 3rd Edition (Java)" by Clifford A. Shaffer Copyright 2008-2011 by Clifford A. Shaffer */ /** Singly linked list node with freelist support */ class Link { private E element; // Value for this node private Link next; // Point to next node in list /** Constructors */ Link(E it, Link nextval) { element = it; next = nextval; } Link(Link nextval) { next = nextval; } /** Get and 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 */ static Link freelist = null; // Freelist for the class /** @return A new link */ static Link get(E it, Link nextval) { if (freelist == null) return new Link(it, nextval); // Get from "new" Link temp = freelist; // Get from freelist freelist = freelist.next(); temp.setElement(it); temp.setNext(nextval); return temp; } /** Return a link to the freelist */ void release() { element = null; // Drop reference to the element next = freelist; freelist = this; } }