Queue link Storage Structure-blockchain queue illustration and code implementation

Source: Internet
Author: User

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 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.