queue
A linear table with limited operation. It can only be inserted at one end of the table and deleted at the other end of the table. Feature: first in, first out
Team leader: the end that can be deleted, also known as the team leader.
End of line: the end that allows insertion
1. Sequential storage structure:
Focus on circular queue: in order to solve the problem of false overflow and make full use of the queue, the sequential queue is imagined as a ring space, that is, the table storing queue elements is logically regarded as a ring, which is called circular queue.
In a non empty loop queue, the queue head pointer always points to the current queue head element, and the queue tail pointer always points to the unit after the real queue tail element
When the first pointer of the queue is Q.font=Q.MaxSize-1, it will automatically reach 0 when it advances another position, which can be realized by division and remainder operation.
Initial: Q.front=Q.rear=0
Queue head pointer 1:Q.front=(Q.front+1)%MaxSize
End of queue pointer 1: Q.rear=(Q.rear+1)%MaxSize
Queue length: (Q.rear+MaxSize-Q.front)%MaxSize
When leaving the team and joining the team: the pointer enters 1 clockwise
Air judgment: Q.front=Q.rear
Team full: 1. Lose an element space. When the successor unit of the empty unit pointed to by the end of the team pointer is the unit where the team head element is located, it will stop joining the team. Team full: (Q.rear+1)%MaxSizeQ.front
2. Add a data unit indicating the number of elements. Queue empty: Q.size=0; Team full: Q.size=MaxSize
3. Add a tag data member. During initialization: rear=front=tag=0; Each deletion succeeds: tag=0; Successful insertion each time: tag=1;
Queue full: frontrear & & tag1 queue empty: frontrear & & tag = 0, deletion can be empty, and insertion can be full.
#include "stdio.h" #include "stdlib.h" typedef int ElemType; #define MaxSize 50 #define true 1 #define false 0 typedef struct { ElemType data[MaxSize];//Store queue elements int front, rear;//Queue head pointer and queue tail pointer }SqQueue; //initialization void InitQueue(SqQueue* Q) { Q->rear = Q->front = 0; } //Judgment null int IsEmpty(SqQueue Q) { if (Q.rear == Q.front) return true; else return false; } //Join the team int EnterQueue(SqQueue *Q, ElemType x) { if ((Q->rear + 1) % MaxSize == Q->front)//When the team is full, an error is reported return false; Q->data[Q->rear] = x; Q->rear = (Q->rear + 1) % MaxSize;//End of line pointer plus 1 to take mold return true; } //Out of the team int DeQueue(SqQueue* Q, ElemType* x) { if (Q->rear == Q->front)//If the team is empty, an error is reported return false; x = Q->data[Q->front]; Q->front = (Q->front + 1) % MaxSize;//Team head pointer plus 1 to take mold return true; }
2. Chain queue
If the linked list structure of the leading node is adopted: the head pointer always points to the head node, the tail pointer points to the last element, and the head pointer and tail pointer of the empty chain queue point to the head node.
typedef struct Node{//Chain queue node ElemType data; struct Node *next; }LinkQueueNode; typedef struct{//Chain queue LinkQueueNode *front, * rear;//Queue head and tail pointers }LinkQueue; int Initqueue(LinkQueue* Q) { Q->front = (LinkQueueNode*)malloc(sizeof(LinkQueueNode)); if (Q->front != NULL) { Q->rear = Q->front; Q->front->next = NULL; return true; } else return false; } //Chain team included in the team int EnterLinkQueue(LinkQueue *Q, ElemType x) { 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; } //Chain team list team int DeleteQueue(LinkQueue* Q, ElemType* x) { LinkQueueNode* p; if (Q->front == Q->rear) return false; p = Q->front->next; Q->front->next = p->next;//Team leader element p out of the team if (Q->rear == p)//If there is only one element p in the queue, the queue will be empty after p is out of the queue Q->rear = Q->front; *x = p->data; free(p); return true; }