#include using namespace std; template class Node { public: Node(T t):data(t), next(NULL){}; T getdata() {return data;}; Node *next; // The client program (list) needs to maintain the link, //so public. private: T data; // We don't want the data to be changed directly. }; /////////////////////////////////////////////////////////////////// //////////// Stack Class Template /////////////////////////////// /////////////////////////////////////////////////////////////////// template class Stack { public: Stack(); Stack(Stack & st); // Important!! void push(T data); T pop(); bool isempty(); ~Stack(); // Delete every node in the list. friend Node *peek_top(Stack & s); // This is not a good idea. private: Node *top; }; template Stack::Stack() // Can't go like Stack::Stack() :top(NULL) {} template // Copy constructor Stack::Stack(Stack & st) // Need & st { if (st.top == NULL) { top=NULL; return; } Node *cur, *next, *new_node; cur = new Node((st.top)->getdata()); next=cur->next=(st.top)->next; top = cur; while (next != NULL) { new_node = new Node(next->getdata()); next=new_node->next=next->next; cur->next=new_node; cur=new_node; } } template void Stack::push(T data) { Node *new_node; new_node = new Node(data); new_node->next = top; top = new_node; } template T Stack::pop() { T data; if (top==NULL) return data; Node *node=top; data=top->getdata(); top = top->next; delete node; return data; } template bool Stack::isempty() { return (top==NULL); } template Stack::~Stack() // Destructor { while (top!=NULL) pop(); } template Node *peek_top(Stack & s) { return s.top; } /////////////////////////////////////////////////////////////////// //////////// Queue Class Template /////////////////////////////// /////////////////////////////////////////////////////////////////// template class Queue { public: Queue(); Queue(Queue & st); // Copy Constructor void push(T data); T pop(); bool isempty(); ~Queue(); // Delete every node in the list. private: Node *head, *tail; }; template Queue::Queue() :head(NULL), tail(NULL){} template // Copy constructor Queue::Queue(Queue & st) // Need & st { if (st.head == NULL) { head=tail=NULL; return; } Node *cur, *next, *new_node; cur = new Node((st.head)->getdata()); next=cur->next=(st.head)->next; head = tail = cur; while (next != NULL) { new_node = new Node(next->getdata()); next = new_node->next = next->next; tail = cur = cur->next = new_node; } } template void Queue::push(T data) { Node *new_node; new_node = new Node(data); new_node->next = head; head = new_node; if (tail == NULL) tail = head; } template T Queue::pop() { T data; if (head==NULL) return data; Node *new_tail=head; // The node before tail if (head == tail) head = new_tail = NULL; // No node before tail else /// has more than one node; while (new_tail->next != tail) new_tail = new_tail->next; data = tail->getdata(); delete tail; tail = new_tail; return data; } template bool Queue::isempty() { return (head==NULL); } template Queue::~Queue() // Destructor { while (head!=NULL) pop(); }