Ordered List ADT Implemented With Pointers Using a Head Node and a Trailer
Node

The above ordered list has one item. This item's key is
3.
The header node has a key of -¥ ( INT_MIN ). The trailer node has a key of +¥ ( INT_MAX ).
orderedlist.h
#ifndef ORDERED_LIST_H
#define ORDERED_LIST_H
#include <stddef.h>
#include <limits.h>
struct
Info
{
...
};
class OrderedList {
public:
OrderedList();
~OrderedList();
OrderedList(
/* in */ const OrderedList& srcOrderedList
);
bool Search( /* in */ int key, /* out */ Info& info );
bool Insert( /* in */ int key, /* in */ Info info );
bool Delete( /* in */ int key );
int Size() const;
void Reset();
void Advance( /* out */ int& key,
/* out */ Info& info );
bool IsEmpty()
const;
bool IsFull()
const;
private:
struct Node
{
int key;
Info info;
Node*
next;
};
Node*
front;
int size;
Node*
current;
};
#endif
orderedlist.cpp
// Pointer implementation of an ordered list.
// Uses a header node and a trailer node.
#include "OrderedList.h"
OrderedList::OrderedList()
{
front = new Node;
front->key = INT_MIN;
front->next = new Node;
front->next->key = INT_MAX;
front->next->next = NULL;
size = 0;
}
OrderedList::~OrderedList()
{
Node* oldPtr;
while ( front != NULL )
{
oldPtr = front;
front =
front->next;
delete oldPtr;
}
}
OrderedList::OrderedList( /* in */ const OrderedList&
srcOrderedList )
{
front = new Node;
front->key = INT_MIN;
front->next = new Node;
front->next->key = INT_MAX;
front->next->next = NULL;
Node* srcPtr = srcOrderedList.front->next;
Node* rear
= front;
Node* newPtr;
size = srcOrderedList.size;
for ( int i
= 0; i < size; i++ )
{
newPtr = new Node;
newPtr->key = srcPtr->key;
newPtr->info = srcPtr->info;
newPtr->next = rear->next;
rear->next = newPtr;
rear = newPtr;
srcPtr = srcPtr->next;
}
}
bool OrderedList::IsEmpty()
const
{
return (size == 0);
}
bool OrderedList::IsFull() const
{
return false;
}
bool OrderedList::Search( /* in
*/ int key, /* out */ Info& info )
{
for ( Node* location = front->next;
key > location->key;
location = location->next )
/* do
nothing */ ;
// key
<= location->key
if ( key == location->key )
{
info = location->info;
return true;
}
else
return false;
}
bool OrderedList::Insert( /* in */ int key, /* in */ Info info
)
{
Node*
backup = front;
for ( Node* location = front->next;
key > location->key;
location
= location->next )
backup = location;
// key
<= location->key
if ( key == location->key )
return false;
else
{
// Create and fill in the new node
Node* newPtr = new Node;
newPtr->key = key;
newPtr->info = info;
// Insert
between *backup and *location
newPtr->next = location;
backup->next = newPtr;
size++;
return true;
}
}
bool OrderedList::Delete( /* in */ int
key )
{
Node*
backup = front;
for ( Node* location = front->next;
key > location->key;
location = location->next )
backup = location;
// key
<= location->key
if ( key == location->key )
{
backup->next = location->next;
delete location;
size--;
return true;
}
else
return false;
}
int OrderedList::Size() const
{
return size;
}
void OrderedList::Reset()
{
current = front->next;
}
void OrderedList::Advance( /* out */ int& key, /* out */ Info& info )
{
key = current->key;
info =
current->info;
current = current->next;
}