#ifndef _LINKEDLIST_H_ #define _LINKEDLIST_H_ #include "Node.h" // contains the node information #include // for using cout using namespace std; template // templated class class LinkedList { public: LinkedList(); // constructor ~LinkedList(); // deconstructor void appendNode(const T &); // add a node to end of list bool empty(); // if the list is empty int nodeCount(); // how many nodes are in the list private: Node *first; // first element in the list Node * newNode(const T &); // helper function; creates a new node }; // constructor (empty body) template LinkedList::LinkedList() : first(NULL) {} // destructor template LinkedList::~LinkedList() { if (!empty()) { // follow the links, clobbering as we go Node *p= first; while (p != NULL) { Node *next = p->next; // retrieve this node's "next" before we clobber it delete p; p = next; }; } } template void LinkedList::appendNode(const T &data) { // allocate a new node, copying supplied data into it. Node *nn = newNode(data); if (empty()) { // make a new node first = nn; } else { // list already has nodes Node *p = first; while (p->next != NULL) p = p->next; // go to end of list p->next = nn; } nn->next = NULL; // be safe } // predicate: is this list empty? template bool LinkedList::empty() { return (first == NULL); } // utility function to create a node template Node *LinkedList::newNode(const T &value) { return new Node(value); } template int LinkedList::nodeCount(void) { int n=0; for(Node *p = first; p != NULL; p = p->next) n++; return n; } #endif