mirror of
https://git.adityakumar.xyz/data-structures.git
synced 2024-11-10 05:39:44 +00:00
38 lines
1.1 KiB
Java
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;
|
|
}
|
|
}
|