home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_08_11
/
8n11087a
< prev
next >
Wrap
Text File
|
1990-09-18
|
2KB
|
60 lines
typedef int T;
// Boolean type
typedef int Truth;
// Node of T is a parametric type.
// A node contains a value of type T. Each node can link
// itself to another node
class Node {
public:
Node( T x) { val = x; Next = 0; }
Node *next() { return Next; }
void link( Node *neighbor) { Next = neighbor; }
T value() { return val; }
private:
Node *Next;
T val;
};
// List of T is a Lisp-style list of T values.
class List {
public:
List() { head = 0; }
~List() {}
Truth isempty() { return head == 0; }
// get returns the value at the head of the List
// without deleting it. This is car in LISP.
// precondition: !isempty() (List is not empty)
T get() { return head->value(); }
// add inserts a new element with value x at the head
// of the List
void add(T x)
{
Node *p = new Node(x); // error return of new should
// be checked ...
if( !isempty() ) // member fun. calls member fun.
p->link( head);
head = p;
}
// del deletes value at the head of the list
// and returns the value
// precondition: !isempty() (List is not empty)
T del()
{
Node *t = head;
T r = head->value();
head = head->next();
delete t;
return r;
}
private:
Node *head;
};