About chained queues
chained queues, also known as chain teams, are implemented using a single-linked list, which requires a head pointer with a tail pointer
Structure diagram:
The elements that the chain needs to make up
/*链式队列的每一个节点*/struct node{ int data;//存储数据 struct node *next;//指向下一个节点的指针};
/*链式队列*/typedefstruct{ struct node *head;//头指针 struct node *tail;//尾指针}LinkedQueue;
Create an empty queue for the lead node
- Create a node P
- Point the P node's next to null
- Create a queue
- Point the head pointer of the queue to node p
- Point the tail pointer of the queue to node p
Create a structure diagram of an empty node (actually created without the P pointer)
/*初始化一个空的链式队列*/LinkedQueue *initLinkedQueue(){ LinkedQueue *linkedQueue = (LinkedQueue *)malloc(sizeof(LinkedQueue));//创建队列 struct node *p = (struct node *)malloc(sizeof(struct node));//创建第一个空节点 p->next = NULL;//让第一个节点的next指向null linkedQueue->head = linkedQueue->tail = p;//将队列的head和tail指针指向第一个节点 return linkedQueue;}
Queued operation
For chained queues do not consider the queue full of dissatisfaction, because never full
1. Create a new node Q
2. Assign the incoming data to the value of Q
3. The next pointer of Q points to null
4. Point the tail pointer of the queue to the node's next pointer to the Q node
5. The tail pointer to the queue points to the newly inserted Q node, tail always points to the last node
/* Join the operation * /voidInsert (Linkedqueue*LQ, int num) {struct node*Q=(struct node*) malloc (sizeof (struct node));//Request a new node in memoryQ -Data =Num//Incoming data to the new nodeQ -Next= NULL;//The next pointer of the new node is emptyLq -Tail -Next=Q//The next node of the node referred to by the tail node points to the new nodeLq -Tail=Qthe//tail pointer points to the newly inserted node}
Out of team operation
- Declaring a node Q
- If the queue is not empty, ask Q to point to the node that is behind the head node, that is, the node to be deleted
- Let next of the head node of the queue point to the node behind the node that the Q refers to
- Save the data of the node referred to by Q.
- Destroys the node that the Q refers to (free (q))
- Note that if only the last node is out of the queue, the node memory that the tail pointer refers to has been recycled, so the tail pointer needs to be processed so that the head pointer =tail the pointer
/ * Delete queue elements * /int Delete (linkedqueue*LQ, int*temp) {struct node*Qif(IsEmpty (LQ)) {printf ("Queue is empty!\n");return 0; }Else{Q=Lq -Head -Next//Let Q point to the node behind the head nodeLq -Head -Next=Q -Next//Let next of the head node of the queue point to the node at the back of the node referred to by q *Temp=Q -Data;//Save the data of the node referred to by Q.Free (q);//Destroy the node referred to by Q, Reclaim memory //When there is only one element, the queue is empty and the tail pointer is modified at this time if(LQ -Head -Next== NULL) {LQ -Head=Lq -Tail }return 1; }}
Using the C language for chained queues
#include <stdio.h>#include <stdlib.h>/* Each node of a chained queue */structnode{intDatastructnode *next;};/ * Chained queue * /typedef struct{structNode *head;structNode *tail;} Linkedqueue;/ * Initialize an empty chained queue * /Linkedqueue *initlinkedqueue () {Linkedqueue *linkedqueue = (Linkedqueue *)malloc(sizeof(Linkedqueue));structNode *p = (structNode *)malloc(sizeof(structnode)); P->next = NULL; Linkedqueue->head = Linkedqueue->tail = P;returnLinkedqueue;}/* Join the operation * /voidInsert (Linkedqueue *LQ,intNUM) {structNode *q = (structNode *)malloc(sizeof(structnode));//Request a new node in memoryQ->data = num;//Incoming data to the new nodeQ->next = NULL;//The next pointer of the new node is emptyLq->tail->next = q;//The next node of the node referred to by the tail node points to the new nodeLq->tail = q;the//tail pointer points to the newly inserted node}/ * Determine if the queue is empty * /intIsEmpty (Linkedqueue *lq) {if(Lq->head = = Lq->tail) {//If the tail pointer points to one, the queue is empty return 1; }Else{return 0; }}/ * Delete queue elements * /int Delete(Linkedqueue *LQ,int*temp) {structNode *q;if(IsEmpty (LQ)) {printf("Queue is empty!\n");return 0; }Else{q = lq->head->next; Lq->head->next = q->next; *temp = q->data; Free(q);//When there is only one element, the queue is empty and the tail pointer is modified at this time if(Lq->head->next = = NULL) {Lq->head = lq->tail; }return 1; }}/ * Traverse queue * /voidDisplay (Linkedqueue *lq) {structNode *q = lq->head->next; while(q! = NULL) {printf("queue element is%d\n", Q->data); Q = q->next; }}intMain () {Linkedqueue *LQ = Initlinkedqueue (); Insert (LQ,1); Insert (LQ,2); Insert (LQ,3); Insert (LQ,4); Insert (LQ,5);inttemp =0;Delete(LQ, &TEMP);printf("%d\n", temp); Display (LQ);return 0;}
Data structure-queue chain queue