Link storage structure of the queue-blockchain queue
Illustration:
Linkqueue. h
// Linkqueue. h # ifndef linkqueue_h # define linkqueue_htemplate <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 head and end of the team, pointing to the head node and terminal node respectively}; # endif;
Linkqueue. 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, an element is removed from the team header */ 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 element of the queue header-> next = p-> next; // 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;}/** preconditions: the queue already exists * input: No * function: Read the element in the queue header * output: If the queue is not empty, element of the returned team header * 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 ;}