Data structure (C implementation) ------- chain queue, ------- queue
A chain queue is a chain storage structure of a queue. It is a single-chain table that is deleted only at the header and inserted at the end of the table, therefore, a chain queue needs to set two pointers to indicate the first element and the last element respectively. To facilitate the operation, add a header node to the chain queue and direct the header pointer to the first node, therefore, the null chain queue is determined by the head pointer and tail pointer pointing to the head node.
Type description of the blockchain queue:
// Chain queue type description typedef int QElemType; typedef struct node {QElemType data; struct node * next;} QNode, * QueuePtr; typedef struct {QueuePtr front; // team head pointer QueuePtr rear; // team end pointer} LinkQueue;
Basic operations:
1. initialize the chain Queue (Leading node) Init_LinkQueue (LinkQueue * Q)
// Initialize the chain Queue (Leading node) void Init_LinkQueue (LinkQueue * Q) {QueuePtr head = (QueuePtr) malloc (sizeof (QNode); if (! Head) exit (OVERFLOW); head-> next = NULL; Q-> front = Q-> rear = head ;}
2. Destroy the chain queue Destroy_LinkQueue (LinkQueue * Q)
// Destroy the chain queue void Destroy_LinkQueue (LinkQueue * Q) {// release all nodes in the chain queue from the Start Node while (Q-> front) {Q-> rear = Q-> front-> next; free (Q-> front); Q-> front = Q-> rear ;}}
3. Clear the chain queue Clear_LinkQueue (LinkQueue * Q)
// Clear the chain queue void Clear_LinkQueue (LinkQueue * Q) {Destroy_LinkQueue (Q); Init_LinkQueue (Q );}
4. Determine whether the chain queue is empty. IsEmpty_LinkQueue (LinkQueue * Q)
// Determine whether the chain queue is empty. int IsEmpty_LinkQueue (LinkQueue * Q) {return Q-> front = Q-> rear ;}
5. Get the length of the chain queue GetLength_LinkQueue (LinkQueue * Q)
// Obtain the length of the chain queue int GetLength_LinkQueue (LinkQueue * Q) {int count = 0; // point to the first node of data storage, QueuePtr p = Q-> front-> next; while (p) {count ++; p = p-> next;} return count ;}
6. Get the Header element GetHead_LinkQueue (LinkQueue * Q, QElemType * x) of the chain queue)
// Obtain the Header element void GetHead_LinkQueue (LinkQueue * Q, QElemType * x) {if (IsEmpty_LinkQueue (Q) {printf ("The blockchain queue is empty! \ N "); exit (0);} else {* x = Q-> front-> next-> data ;}}
7. GetRear_LinkQueue (LinkQueue * Q, QElemType * x)
// Obtain the Header element void GetRear_LinkQueue (LinkQueue * Q, QElemType * x) {if (IsEmpty_LinkQueue (Q) {printf ("the chain queue is empty! \ N "); exit (0);} else {* x = Q-> rear-> data ;}}
8. En_LinkQueue (LinkQueue * Q, QElemType x)
// Void En_LinkQueue (LinkQueue * Q, QElemType x) {QueuePtr q = (QueuePtr) malloc (sizeof (QNode); if (! Q) exit (OVERFLOW); q-> data = x; q-> next = NULL; Q-> rear-> next = q; Q-> rear = q ;}
9. De_LinkQueue (LinkQueue * Q, QElemType * x)
// Out-of-chain queue void De_LinkQueue (LinkQueue * Q, QElemType * x) {QueuePtr q; if (IsEmpty_LinkQueue (Q) {printf ("the chain queue is empty! \ N "); exit (0);} else {* x = Q-> front-> next-> data; q = Q-> front-> next; * x = q-> data; Q-> front-> next = q-> next; // The queue is empty after an element is deleted. if (q-> next = NULL) q-> rear = Q-> front; free (q );}}
10. Output chain queue Print_LinkQueue (LinkQueue * Q)
// Output chain queue void Print_LinkQueue (LinkQueue * Q) {// p points to the next node of the header node, that is, QueuePtr p = Q-> front-> next; if (IsEmpty_LinkQueue (Q) {printf ("the chain queue is empty! \ N "); exit (0) ;}else {while (p) {printf (" % d \ t ", p-> data ); p = p-> next;} printf ("\ n ");}}
Data Structure-chain queue, implemented in C Language
# Include <stdio. h>
# Include <stdlib. h>
Struct node
{
Int data;
Struct node * next;
};
Typedef struct node;
Int ifnull (Node * tail)
{
If (tail-> next = NULL)
Return 0;
Return 1;
}
Node * qinit (Node * tail)
{
Tail = (Node *) malloc (sizeof (Node ));
Tail-> data = 0;
Tail-> next = NULL;
Return tail;
}
Void push (Node * tail, int data)
{
If (tail = NULL)
{
Perror ("error! ");
Return;
}
Node * tmp = NULL;
If (ifnull (tail) = 0)
Tmp = tail;
Else
Tmp = (Node *) malloc (sizeof (Node ));
Tmp-> data = data;
Tmp-> next = tail-> next;
Tail-> next = tmp;
Tail = tmp;
}
Void pop (Node * tail)
{
If (tail = NULL)
{
Perror ("error! ");
Return;
}
If (ifnull (tail) = 0)
Printf ("queue is empty! ");
If (tail-> next = tail)
{
Free (tail );
Tail = NULL;
Return;
}
Node * tmp = tail-> next;
While (1)
{
If (tmp-> next = tail)
{
Node * n = tail;
Tail = tmp;
Tail-> next = n-> next;
Free (n );
Break;
}
Tmp = tmp-> next;
}
}
Node * getdata (Node * tail, int * data)
{
If (tail = NULL)
{
Perror ("error! ");
Return;
}
If (ifnull (tail) = 0)
{
Printf ("queue is empty! ");
Return;
}
If (tail-> next = tail)
{
* Data = tail-> data;
Tail-> data = 0;
Tail-> next = NULL;
Return tail;
}
Node * tmp = tail-> next;
While (1)
{
If (tmp-> next = tail)
{
Node * n = tail-> next;
* Data = tail-> data;
Free (tail );
Tail = tmp;
Tail-> next = n;
Break;
}
Tmp = tmp-> next;
}
Return tail; ...... the remaining full text>
The implementation of the basic operations of chain queue in the data structure C or C ++ helps me find the problem of the program under the blockchain
# Include <iostream>
Using namespace std;
# Define true 1
# Define false 0
Typedef struct Node // define a queue
{
Int data;
Struct Node * next;
} LinkQueueNode;
Typedef struct
{
LinkQueueNode * front;
LinkQueueNode * rear;
} LinkQueue;
Void InitQueue (LinkQueue * Q) // initialize a queue. The declared queue object is used as the initialization parameter, and local declared variables cannot be used.
{
Q-> front = Q-> rear = (LinkQueueNode *) malloc (sizeof (LinkQueueNode ));
Q-> front-> next = NULL;
}
Void CreatQueue (LinkQueue * Q) // create a queue
{
Int I, n;
LinkQueueNode * s;
Cout <"number of elements in the input queue n" <endl;
Cin> n;
Cout <"elements in the input queue" <endl;
For (I = 0; I <n; I ++)
{
S = (LinkQueueNode *) malloc (sizeof (LinkQueueNode); // space to be allocated
Cin> s-> data;
Q-> rear-> next = s;
Q-> rear = s;
// S = s-> next; useless
}
Q-> rear-> next = NULL;
}
Int EnterQueue (LinkQueue * Q, int x) // queue operations
{
LinkQueueNode * NewNode;
NewNode = (LinkQueueNode *) malloc (sizeof (LinkQueueNode ));
If (NewNode! = NULL)
{
NewNode-> data = x;
NewNode-> next = NULL;
Q-> rear-> next = NewNode;
Q-> rear = NewNode;
Return (true );
}
Else
Return (false );
}
Void DeleteQueue (LinkQueue * Q) // operate on
{
Int data;
LinkQueueNode * p;
If (Q-> front = Q-> rear)
{
Cout <"queue is empty" <endl;
}
Else
{
P = Q-> front-> next;
Q-> front-> next = p-> next;
Data = p-> data;
Free (p );
Cout <data <endl;
}
}
Void Output (LinkQueue * Q) // Output team ...... remaining full text>