Question: Implement a collection of freelists for variable-length strings, as described at the end of Section 4.1.2. For each such freelist, you will need an access
Implement a collection of freelists for variable-length strings, as described at the end of Section 4.1.2.

For each such freelist, you will need an access function to get it if it exists, and implement it if it does not. A major design consideration is how to organize the collection of freelists, which are distinguished by the length of the strings. Essentially, what is needed is a dictionary of freelists, organized by string lengths.
// 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; } = Link next() { return next; } Link setNext (Link nextval) { return next = nextval; } E element () { return element; } E setElement (E it) { return element = it; } // Extensions to support freelists static Link freelist = null; // Get new link static Link get (E it, Link nextval) { if (freelist == null) return new Link (it, nextval); // Get from "new" Link temp = freelist; freelist = freelist.next(); // Get from freelist temp.setElement (it); temp.setNext (nextval); return temp; // Freelist for the class } void release () { element = null; next = freelist; free list this; } } // class Link // Return Link to freelist // Drop reference to the element
Step by Step Solution
3.50 Rating (153 Votes )
There are 3 Steps involved in it
A freelist is a data structure that maintains a pool of reusable objects such as strings to avoid frequent memory allocation and deallocation A collection of freelists for variablelength strings is a ... View full answer
Get step-by-step solutions from verified subject matter experts
