Link storage structure of the queue-link queue diagram:
LinkQueue. h [cpp] // LinkQueue. h # ifndef LINKQUEUE_H # define LINKQUEUE_H template <class T> struct Node {T data; Node <T> * next; // here <T> can also be omitted }; template <class T> class LinkQueue {public: LinkQueue (); // constructor to initialize an empty chain queue ~ LinkQueue (); // destructor to release the storage space void EnQueue (T x) of each node in the chain queue; // Add element x to the queue T DeQueue (); // exit the queue Header element T GetQueue (); // The queue Header element bool Empty (); // determine whether the queue is Empty. private: node <T> * front, * rear; // The frontend and backend pointers to the header and terminal nodes respectively.}; # endif; LinkQueue. cpp [cpp] // LinkQueue. cpp # include "LinkQueue. h "/** preconditions: the queue does not exist * input: No * function: Initialize the queue * output: No * post condition: create an empty queue */template <class T> LinkQueue <T>: LinkQueue () {Node <T> * s; s = new Node <T >; S-> next = NULL; front = rear = s;}/** prerequisites: the queue has * input: No * function: Destroy queue * output: no * post condition: Release the storage space occupied by the queue */template <class T> LinkQueue <T> ::~ LinkQueue () {while (front) {Node <T> * p; p = front-> next; delete front; front = p ;}}/** prerequisites: the queue already has * input: element value s * function: insert an element at the end of the Team * input: none * post condition: If the insertion is successful, added an element */template <class T> void LinkQueue <T>: EnQueue (T x) {Node <T> * s; s = new Node <T>; s-> data = x; // apply for a node whose data field is x, s-> next = NULL; rear-> next = s; // insert node s to the end of the team rear = s;}/** prerequisites: the queue already exists * input: No * function: Delete the Header element * output: if the deletion is successful, the deleted element value is returned. Otherwise, an exception is thrown. * post condition: If the deletion is successful The team header removes an element */template <class T> T LinkQueue <T>: DeQueue () {Node <T> * p; int x; if (rear = front) throw "underflow"; p = front-> next; x = p-> data; // store the front-> next = p-> next element of the queue header; // link the node where the Header element is located. if (p-> next = NULL) rear = front; // determine whether the length of the queue before departure is 1 delete p; return x;}/** prerequisites: the queue already exists * input: No * function: read the queue Header element * output: If the queue is not empty, return the queue Header element * post condition: the queue remains unchanged */template <class T> T LinkQueue <T>: GetQueue () {if (front! = Rear) return front-> next-> data;}/** prerequisites: the queue already exists * input: No * function: determines whether the queue is empty * output: if the queue is Empty, 1 is returned. Otherwise, 0 * post condition is returned: the queue remains unchanged */template <class T> bool LinkQueue <T >:: Empty () {if (front = rear) return 1; else return 0 ;}