DS Chain Team
Blockchain Overview
The queue in the chain is short for the chain team. A chain queue clearly requires two pointers (Head pointer and tail pointer) to indicate the head and tail of the team respectively to be uniquely identified. For ease of operation, we also add a header node to the chain queue as we add the header node to the single-chain table of a linear table and point the header pointer to the header node. Therefore, the judgment condition of the empty chain queue is that both the header pointer and the tail pointer point to the header node.
The operations of the chain queue are the insertion and deletion operations of a single-chain table.
Element x incoming queue
Element y in queue
Element x output queue
Basic operations
1. Prerequisites
# Include using namespace std; # include # define OK 1 # define ERROR 0 # define OVERFLOW-2 # define TRUE 1 # define FALSE 0 # define INFEASIBLE-1 typedef int Status; // re-define Status as int type typedef int QElemType; // re-define QElemType as int type typedef struct QNode {// re-define a node Structure QElemType data; struct QNode * next ;} QNode, * QueuePtr; typedef struct {// defines a chain team QueuePtr front; // The team head pointer QueuePtr rear; // team end pointer} LinkQueue; // a schema variable defined
1. initialize the queue.
<Span style = "font-size: 18px;"> </span> // 1 initialize the queue Status InitQueue (LinkQueue & Q) {Q. front = Q. rear = (QueuePtr) malloc (sizeof (QNode); if (! Q. front) {exit (OVERFLOW);} Q. front-> next = NULL; return OK ;}
2. Determine whether the blockchain team is empty.
// 2 check whether the Chain team is empty. Status QueueEmpty (LinkQueue Q) {return (Q. front = Q. rear );}
3. Length of the Chain Team
// 3 determine the queue length Status QueueLength (LinkQueue Q) {int I = 0; QueuePtr p = Q. front; while (p! = Q. rear) {I ++; p = p-> next;} return I ;}
4. Retrieve the element from the queue Header
// 4 retrieve the Header element Status GetHead (LinkQueue Q, QElemType & e) {if (Q. front = Q. rear) {return ERROR;} e = Q. front-> next-> data; return OK ;}
5. Clear the queue
// 5 clear the queue Status ClearQueue (LinkQueue & Q) {Q. rear = Q. front; QueuePtr p = Q. front-> next; Q. front-> next = NULL; while (p) {QueuePtr q = p-> next; free (p); p = q;} return OK ;}
6. Destroy a queue
// 6 destroy the queue Status DestroyQueue (LinkQueue & Q) {while (Q. front) {Q. rear = Q. front-> next; free (Q. front); Q. front = Q. rear;} return OK ;}
7 inbound queue
// 7 inbound queue Status EnQueue (LinkQueue & Q, QElemType e) {QueuePtr p = (QueuePtr) malloc (sizeof (QNode); if (! P) {exit (OVERFLOW);} p-> data = e; p-> next = NULL; Q. rear-> next = p; Q. rear = p; return OK ;}
8-out queue
// 8 output queue Status DeQueue (LinkQueue & Q, QElemType & e) {if (Q. front = Q. rear) {return ERROR;} QueuePtr p = (QueuePtr) malloc (sizeof (QNode); p = Q. front-> next; e = p-> data; Q. front-> next = p-> next; if (Q. rear = p) {Q. rear = Q. front;} free (p); return OK ;}