data-structures/java/SList/Freelist.java
2022-09-12 18:47:38 +05:30

38 lines
1.1 KiB
Java

/** Singly linked list node with freelist support */
class Link<E> {
private E element; // Value for this node
private Link<E> next; // Point to next node in the list
/** Constructors */
Link (E it, Link<E> nextval)
{ element = it; next = nextval; }
Link (Link<E> nextval) { next = nextval; }
/** Get and set methods */
Link<E> next() { return next; }
Link<E> setNext(Link<E> nextval) { return next = nextval; }
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 <E> Link<E> get(E it, Link<E> nextval) {
if (freelist == null)
return new Link<E> get(E it, Link<E> nextval); // Get from "new"
Link<E> 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;
next = freelist;
freelist = this;
}
}