template class LQueue { private: class QNode { public: T data; QNode *next; QNode (T data, QNode *next) { this -> data = data; this -> next = next; } }; int capacity, count; QNode *head, *tail; public: // creates an empty queue of the given size. // quits if the capacity is unreasonable. LQueue (int capacity); // enqueues the item at the end of the queue. // quits if no space for item. void enqueue (T item); // dequeues and returns the item from the front of the queue. // quits if queue is empty. T dequeue (); // returns number of items in queue int size () const; // returns the item from the front of the queue, without changing the queue. //quits if queue is empty. T look () const; }; // creates an empty queue of the given size. // quits if the capacity is unreasonable. template LQueue::LQueue (int capacity) { head = tail = NULL; count = 0; if (capacity < 1 ) { // initialize fields and quit this -> capacity = 1; quit("LQueue::LQueue - capacity too small\n"); } this -> capacity = capacity; } // enqueues the item at the end of the queue. // quits if no space for item. template void LQueue::enqueue (T item) { if (count == capacity) quit("LQueue::enqueue - queue full\n"); if (tail == NULL) { // queue empty tail = head = new QNode (item, NULL); } else { tail -> next = new QNode (item, NULL); tail = tail -> next; // could be combined with previous statement } count++; } // dequeues and returns the item from the front of the queue. // quits if queue is empty. template T LQueue::dequeue () { T result; QNode *c; if (count == 0) quit("LQueue::dequeue - queue empty\n"); c = head; result = c -> data; head = head -> next; if (head == NULL) tail = NULL; // queue now empty count--; delete c; return result; } // returns number of items in queue template int LQueue::size () const { return count; } // returns the item from the front of the queue, without changing the queue. // quits if queue is empty. template T LQueue::look () const { if (count == 0) quit("LQueue::look - queue empty\n"); return head->data; }