CPS 100, Spring 1997, Self-Organizing Lists (group work)
In a self-organizing linked list, each time a
successful search is performed, the node found is moved one place
closer to the front of the list. The idea is that frequently accessed
nodes will congregate near the front and be found more quickly. For the
purposes of this exercise, assume that linked lists are implemented using
the declaration below (for doubly-linked lists).
struct Node
{
Node (const string & s,
Node * follow = 0, Node * before = 0)
: info(s),
next(follow),
previous(before)
{}
// data
string info;
Node * next;
Node * previous;
};
Assume that there is a header node, write the function
SelfOrgSearch below.
Node * SelfOrgSearch(Node * list, const string & key)
// precondition: list has a header node
// postcondition: returns pointer to first node containing key
// returns 0 if key not contained in any node
// if key found, node found is moved one place closer to front
Owen L. Astrachan
Last modified: Mon Feb 3 12:09:11 EST