MAG - CGAZV5N1.ZIP - NODE.HPP

 
Output of file : NODE.HPP contained in archive : CGAZV5N1.ZIP

// NODE.HPP: place to put things (a singly-linked list w/ no reordering)
// (c) 1990 C Gazette. See notice in NODE.CPP for terms
#ifndef __NODE_HPP__
#define __NODE_HPP__
#include
#include // definition of NULL
#include // NULL for MSC 5.1 (argh!)

// A node has an identity and many connections:
struct node
{
char * identity;
struct holder // hidden in C++ release 2.1 (not Zortech)
{
holder * next; // link to next holder
node * data; // pointer to actual data
holder(holder * nxt, node * dat) // constructor
: next(nxt), data(dat) {}
} *head, *cursor;
// constructor:
node(char * id) : head((holder *)NULL), cursor((holder *)NULL)
{
identity = new char[strlen(id) + 1];
strcpy(identity, id);
}
// NOTE THE OWNERSHIP PROBLEM here:
~node()
{
delete identity;
cursor = head; // delete the list, but not the node contents:
while(cursor)
{
cursor = cursor->next;
delete head;
head = cursor;
}
}
// Paste a new node in at the top (this is tricky):
node * add(node * i)
{
head = new holder(head, i);
return i;
}
// top() and next() return the current node or null, for the list's end:
node * top() // go back to the top of the list
{
cursor = head;
// the list may be empty; only return data if cursor isn't null
return cursor ? cursor->data : (node *)NULL;
}
node * next()
{ // Only move forward if cursor isn't null:
if(cursor)
cursor = cursor->next;
// only return data if cursor isn't null:
return cursor ? cursor->data : (node *)NULL;
}
node * next_valid(); // always returns a valid node * (never NULL)
void remove(node * nd); // remove a node
void conditional_pop(); // remove the top node unless it's the only one
void print(); // list contents of this node
void copy(node * rv); // copy rv's list to our list (for starting searches)
// does node list contain this characteristic?
node * contains(char * string);
// does node list contain a connection to the argument node?
int connected(node * n);
void intersect(node * n); // create intersection between this and n
};

// display the sn node list; return the node the user selects
node * selection_menu(node * sn, char * msg = "");

#endif // __NODE_HPP__