Two common applications of linear structure
1. Definition
A storage structure that can realize FIFO
2. Classification
(1) Chained queue -- realized by chained list (2) Static queue -- implemented by arrayStatic queues usually have to be circular queues
3. Loop queue
(1) Why static queues must be circular queuesUnlike a linked list, the head of the linked list points to the address before the first element, and the rear in the queue points to the next address of the last element
Because static queues are based on arrays, if circular queues are not used, the space used by the deleted elements will not continue to be used, resulting in a waste of space
(2) Several parameters are needed to determineTwo parameters are required, the first is front and the second is rear;
(3) Meaning of each parameter of circular queueDifferent occasions do not mean
-
Queue initialization
The values of front and rear are both 0;
-
Queue is not empty
The front represents the first element of the queue,
The rear represents the next element of the last valid element of the queue -
queue is empty
The values of front and rear are equal, but not necessarily 0, because they are circular queues;
- Store the value in the location represented by the real
- Change the value of the real to the length of the (real + 1)% array (so you can loop)
Change the value of front to the length of (front+1)% array
(6) How to judge whether the circular queue is emptyIf the values of front and rear are equal, the queue is empty
(7) How to determine whether the circular queue is fullThe value of front can be larger or smaller than the real
Method 1: add one more identification parameter to represent the number of elements, which is not commonly used because one more parameter needs to be maintained
Method 2: put one less element (suppose there are 6 elements, and put 5 elements to make it full)
Because if you put all of them, then when they are full, they will be rear = front, and when they are empty, they will be less than one
if((rear+1)% array length = = front) already expired else dissatisfaction4. Circular queue algorithm
(1) Main function and function declaration#include <stdio.h> #include <malloc.h> typedef struct Queue { int* pBase; //Queue first address int len; //queue length int front; //Team leader int rear; //The end of the team }Queue, * pQueue; void init(pQueue); //initialization bool enter_queue(pQueue pQ, int val); //Join the team bool out_queue(pQueue pQ, int* val); //Out of the team void show_queue(pQueue pQ); //ergodic bool full_queue(pQueue pQ); //Judge whether it is full bool emput_queue(pQueue pQ); //Judge whether it is empty int main() { int val; Queue Q; init(&Q); printf("Please enter the entry element as:"); scanf_s("%d",&val); if (enter_queue(&Q, val)) //Join the team printf("The elements of your team are:%d\n", val); else printf("Failed to join the team, the queue is full\n"); printf("Please enter the entry element as:"); scanf_s("%d", &val); if (enter_queue(&Q, val)) //Join the team printf("The elements of your team are:%d\n", val); else printf("Failed to join the team, the queue is full\n"); printf("Please enter the entry element as:"); scanf_s("%d", &val); if (enter_queue(&Q, val)) //Join the team printf("The elements of your team are:%d\n", val); else printf("Failed to join the team, the queue is full\n"); show_queue(&Q); //ergodic if(out_queue(&Q, &val)) //Out of the team printf("Successful team leaving, the elements of your team leaving are:%d\n",val); else printf("Failed to leave the queue, the queue is empty\n"); show_queue(&Q); return 0; }(2) Assign address initialization
void init(pQueue pQ) //initialization { printf("Please enter the queue length:"); scanf_s("%d",&pQ->len); pQ->pBase = (int*)malloc(sizeof(int) * pQ->len); //Dynamic allocation array pQ->front = 0; pQ->rear = 0; }(3) Join the team
bool enter_queue(pQueue pQ, int val) //Join the team from the rear { if (full_queue(pQ)) { return false; } else { pQ->pBase[pQ->rear] = val; //Equivalent to a[i]=val; pQ->rear = (pQ->rear + 1) % pQ->len; //i plus one cycle return true; } }(4) Out of the team
bool out_queue(pQueue pQ, int* val) //Out of the team, from the front { if (emput_queue(pQ)) { return false; } else { *val = pQ->pBase[pQ->front]; pQ->front = (pQ->front + 1) % pQ->len; //i plus one cycle return true; } }(5) Ergodic
void show_queue(pQueue pQ) //ergodic { if (emput_queue(pQ)) { printf("Team empty\n"); } else { int i = pQ->front; //Define a temporary variable printf("Traverse queue:\n"); while (i != pQ->rear) //Every time the output moves backward to the end of the team until it moves to the end of the team { printf("%d\n", pQ->pBase[i]); i = (i + 1) % pQ->len; } } }(6) Judge as full
bool full_queue(pQueue pQ) //Judge whether it is full { if ((pQ->rear + 1) % pQ->len == pQ->front) { return true; } else { return false; } }(7) Judgment is empty
bool emput_queue(pQueue pQ) //Judge whether it is empty { if (pQ->rear == pQ->front) { return true; } else { return false; } }