Data structure (C implementation) ------- chain 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 ");}}