C++ examples for Data Structure:Queue
Template Queue class definition derived from class List
#include <iostream> template <typename NODETYPE> class List;/*from w w w. j a v a 2 s.c om*/ template <typename NODETYPE> class ListNode { friend class List<NODETYPE>; // make List a friend public: explicit ListNode(const NODETYPE &); NODETYPE getData() const; private: NODETYPE data; ListNode<NODETYPE> *nextPtr; }; template <typename NODETYPE> ListNode<NODETYPE>::ListNode(const NODETYPE &info) : data(info), nextPtr(0) {} template <typename NODETYPE> NODETYPE ListNode<NODETYPE>::getData() const { return data; } template <typename NODETYPE> class List { public: List(); ~List(); void insertAtFront(const NODETYPE &); void insertAtBack(const NODETYPE &); bool removeFromFront(NODETYPE &); bool removeFromBack(NODETYPE &); bool isEmpty() const; void print() const; private: ListNode<NODETYPE> *firstPtr; // pointer to first node ListNode<NODETYPE> *lastPtr; // pointer to last node // utility function to allocate new node ListNode<NODETYPE> *getNewNode(const NODETYPE &); }; template <typename NODETYPE> List<NODETYPE>::List() : firstPtr(0), lastPtr(0) {} template <typename NODETYPE> List<NODETYPE>::~List() { if (!isEmpty()) { std::cout << "Destroying Nodes...\n"; ListNode<NODETYPE> *currentPtr = firstPtr; while (currentPtr != 0) { ListNode<NODETYPE> *tempPtr = currentPtr; std::cout << tempPtr->data << '\n'; currentPtr = currentPtr->nextPtr; delete tempPtr; } } } template <typename NODETYPE> void List<NODETYPE>::insertAtFront(const NODETYPE &value) { ListNode<NODETYPE> *newPtr = getNewNode(value); if (isEmpty()) firstPtr = lastPtr = newPtr; // new list only has one node else { newPtr->nextPtr = firstPtr; // point new node to previous list node firstPtr = newPtr; } } template <typename NODETYPE> void List<NODETYPE>::insertAtBack(const NODETYPE &value) { ListNode<NODETYPE> *newPtr = getNewNode(value); if (isEmpty()) firstPtr = lastPtr = newPtr; // new list has only one node else { lastPtr->nextPtr = newPtr; lastPtr = newPtr; } } template <typename NODETYPE> bool List<NODETYPE>::removeFromFront(NODETYPE &value) { if (isEmpty()) // list is empty return false; else { ListNode<NODETYPE> *tempPtr = firstPtr; if (firstPtr == lastPtr) firstPtr = lastPtr = 0; // no nodes remain after removal else firstPtr = firstPtr->nextPtr; // point to previous 2nd node value = tempPtr->data; delete tempPtr; return true; } } template <typename NODETYPE> bool List<NODETYPE>::removeFromBack(NODETYPE &value) { if (isEmpty()) // list is empty return false; else { ListNode<NODETYPE> *tempPtr = lastPtr; if (firstPtr == lastPtr) // list has one element firstPtr = lastPtr = 0; // no nodes remain after removal else { ListNode<NODETYPE> *currentPtr = firstPtr; // locate second to last element while (currentPtr->nextPtr != lastPtr) currentPtr = currentPtr->nextPtr; // move to next node lastPtr = currentPtr; // remove last node currentPtr->nextPtr = 0; // this is now the last node } value = tempPtr->data; delete tempPtr; return true; } } template <typename NODETYPE> bool List<NODETYPE>::isEmpty() const { return firstPtr == 0; } template <typename NODETYPE> ListNode<NODETYPE> *List<NODETYPE>::getNewNode(const NODETYPE &value) { return new ListNode<NODETYPE>(value); } template <typename NODETYPE> void List<NODETYPE>::print() const { if (isEmpty()) { std::cout << "The list is empty\n\n"; return; } ListNode<NODETYPE> *currentPtr = firstPtr; std::cout << "The list is: "; while (currentPtr != 0) { std::cout << currentPtr->data << ' '; currentPtr = currentPtr->nextPtr; } std::cout << "\n\n"; } template <typename QUEUETYPE> class Queue : private List<QUEUETYPE> { public: void enqueue(const QUEUETYPE &data) { this->insertAtBack(data); } bool dequeue(QUEUETYPE &data) { return this->removeFromFront(data); } bool isQueueEmpty() const { return this->isEmpty(); } void printQueue() const { this->print(); } }; #include <iostream> int main(int argc, const char* argv[]) { Queue<int> intQueue; std::cout << "processing an integer Queue" << std::endl; for (int i = 0; i < 3; ++i) { intQueue.enqueue(i); intQueue.printQueue(); } int dequeueInteger; // store dequeued integer while (!intQueue.isQueueEmpty()) { intQueue.dequeue(dequeueInteger); std::cout << dequeueInteger << " dequeued" << std::endl; intQueue.printQueue(); } Queue<double> doubleQueue; double value = 1.1f; std::cout << "processing a double queue" << std::endl; // enqueue floating-point values onto doubleQueue for (int i = 0; i < 3; ++i) { doubleQueue.enqueue(value); doubleQueue.printQueue(); value += 1.1f; } double dequeueDouble; // store dequeued double // dequeue floating-point values from doubleQueue while (!doubleQueue.isQueueEmpty()) { doubleQueue.dequeue(dequeueDouble); std::cout << dequeueDouble << " dequeued" << std::endl; doubleQueue.printQueue(); } return 0; }