Data structure, data structure and Algorithm
Queue chain implementation
1. chained storage representation of a queue
The chain storage structure of a queue is referred to as a chain queue. It is a single-chain table that restricts the deletion operation on the header and the insertion operation at the end of the table.
Two different types of nodes are required: Data Element nodes, the first and last pointer nodes of the queue.
Pointer node type definition:
typedef struct link_queue{ QNode *front , *rear ;}LinkQueue ;
2. blockchain operations and pointer changes
In fact, the operation of a chain is a single-chain table operation, but it is performed by deleting the table header and inserting it at the end of the table. Different pointers are modified during insertion and deletion.
3 basic operations of the chain Queue (1) initialize the chain queue Status Init_LinkQueue (LinkQueue * Q) {/* returns OK, otherwise ERROR */QNode * p; p = (QNode *) malloc (sizeof (QNode);/* Open the header node */if (p = NULL) return ERROR; p-> next = NULL; q-> front = Q-> rear = p; return OK ;}
(2) An Element e is inserted at the end of a known queue to change the team's tail pointer (Q. rear ). Status Insert_LinkQueue (LinkQueue * Q, ElemType e)/* insert Data Element e to the end of queue Q */{QNode * p; p = (QNode *) malloc (sizeof (QNode); if (! P) return ERROR;/* failed to apply for a new node, ERROR flag */p-> data = e; p-> next = NULL; /* form a new node */Q-> rear-> next = p; Q-> rear = p;/* Insert the new node to the end of the Team */return OK ;}
Status Delete_LinkQueue (LinkQueue * Q, ElemType * x) {QNode * p; if (Q-> front = Q-> rear) return ERROR; /* empty Team */p = Q-> front-> next;/* Get the first node of the Team */* x = p-> data; q-> front-> next = p-> next;/* modify the first pointer of the Team */if (p = Q-> rear) q-> rear = Q-> front;/* When the queue has only one node, you should avoid losing the team's tail pointer */free (p); return OK ;}
(4) revoke the void Destroy_LinkQueue (LinkQueue * Q)/* Forward the first element of the Q queue */{while (Q-> front! = NULL) {Q-> rear = Q-> front-> next;/* point the tail pointer to the first node of the queue */free (Q-> front ); /* each time a node is released * // * The first is the header node, and then the Element Node */Q-> front = Q-> rear ;}}