/*--------------------------------------------------------- * File Name: queuelst.cpp * Author: Xiannong Meng * Date: April 21, 1997 * Assignment: Classroom demonstration * Problem statement: This is the templates file for queue class. *--------------------------------------------------------- */ #include "queuelst.h" // Queue constructor // Intent: initialize the queue to be empty // Precondition: none // Postcondition: front and rear points to NULL template Queue::Queue() { front = rear = NULL; } // Insert function // Intent: insert a node that contains value x at the rear of the queue // Precondition: ElmType x has received values // Postcondition: a node containing x is created and inserted into queue template bool Queue::Insert(const ElmType x) { Node * newNode; // make a new node newNode = new Node; newNode->info = x; newNode->link = NULL; if (IsEmpty() == true) // first one on the list { front = newNode; rear = newNode; } else // insert at the end { rear->link = newNode; rear = newNode; } return true; } // Remove // Intent: remove a node at the front of the queue, return its value to x // Precondition: the queue should contain at least one element // Postcondition: one node at the front is removed template bool Queue::Remove(ElmType & x) { if (IsEmpty() == true) { cerr << "queue empty when removing!\n"; return false; } else { Node * temp = front; x = front->info; front = front->link; delete temp; return true; } } // IsEmpty // Intent: test if the queue is empty, note that rear==front is not empty // Precondition: a queue has been initialized // Postcondition: none template bool Queue::IsEmpty() const { return (front == NULL); }