LinkedQueue.h
#ifndef __LINKEDQUEUE_H__ #define __LINKEDQUEUE_H__ template <class T> class Node { public: T data; Node * next; }; template <class T> class LinkedQueue { public: LinkedQueue(); ~LinkedQueue(); public: void enQueue(T _data); T deQueue(); T getQueue(); bool Empty(); private: Node<T> *front, *rear; }; #endif
LinkedQueue.cpp
#include "LinkedQueue.h" template <class T> LinkedQueue<T>::LinkedQueue() { Node<T> * tempNode = new Node<T>; tempNode->next = nullptr; front = tempNode; rear = tempNode; } template <class T> LinkedQueue<T>::~LinkedQueue() { } template <class T> void LinkedQueue<T>::enQueue(T _data) { Node<T> *newNode = new Node<T>; newNode->data = _data; newNode->next = nullptr; rear->next = newNode; rear = newNode; } template <class T> T LinkedQueue<T>::deQueue() { if (Empty()) return false; T data; Node<T> *delNode = new Node<T>; for (delNode = front; delNode->next != rear; delNode = delNode->next); rear = delNode; data = delNode->next->data; delete delNode->next; return data; } template <class T> T LinkedQueue<T>::getQueue() { if (Empty()) return false; Node<T> *getNode = new Node<T>; for (getNode = front; getNode->next != rear; getNode = getNode->next); return getNode->next->data; } template <class T> bool LinkedQueue<T>::Empty() { if (front == rear) return true; return false; }
queue.cpp
#include <iostream> #include "LinkedQueue.h" #include "LinkedQueue.cpp" using namespace std; int main() { LinkedQueue<int> * linQue = new LinkedQueue<int>; linQue->enQueue(1); linQue->enQueue(2); linQue->enQueue(3); cout << linQue->deQueue() << endl; cout << linQue->getQueue() << endl; return 0; }