What is a queue?
Similar to linked lists and stacks, queues are also structures that store data. The order in which the data in the queue enters the queue is very important. Generally speaking, a queue is a group of people or things waiting for service or processing according to the arranged order.
give an example
Take the bank business as an example. In life, when we go to the bank to handle business, we generally need to go to the machine to get the number, and then wait for the counter salesperson to call the number. When you're called, just go and deal with it. However, banks have vip services. People with vip rights and interests can enjoy business processing faster, that is to say, others have higher priority than you. vip privileged channel, you deserve it!
Illustration:
Requirements of task queue:
Using the implemented queue, a random task queue problem is designed and solved. requirement:
① The task consists of task number, task generation time, task duration, task start processing and task processing completion time, etc;
② Tasks are randomly generated by the system; After a task is generated, it is queued for processing in the generated order. A new task can be generated every 5 minutes (not every 5 minutes), and the task duration is within the range of 1 ~ 10 minutes.
③ The system will process the queued tasks in turn.
④ When the system runs for more than a certain time, it will stop generating new tasks;
⑤ Output the processed task information according to the task processing order.
For the selected solution problem, write the test program to complete the solution of the design task, and verify the correctness of the algorithm and program design through the running results.
Here are the basic requirements of task queue:
state invariant
#include <stdlib.h> #include <stdio.h> #Include < time. H > / / header file of time #Include < unistd. H > / / header file of sleep #include <string.h> #define OK 1 #define ERROR 0 #define OVERFLOW –2 #define TRUE 1 #define FALSE 0 typedef int Status;
Task structure
typedef struct Elem{ int TashNum;//task number char TaskGenerTime[50];//Task generation time int TaskTime;//Task duration char TaskStartTime[50];//Task start processing time char TaskEndTime[50];//Task end time }ELemType; //Node structure typedef struct QNode{ ELemType data; struct QNode *next; }QueueNode1,*QueueNode; //Linked list structure of queue typedef struct{ QueueNode front;//Team leader QueueNode rear;//Opposite tail }LinkQueue;
Basic operation of task queue
//Node structure typedef struct QNode{ ELemType data; struct QNode *next; }QueueNode1,*QueueNode; //Linked list structure of queue typedef struct{ QueueNode front;//Team leader QueueNode rear;//Opposite tail }LinkQueue; //Initialize queue Status QueueInit(LinkQueue *Queue); //Destroy queue Status QueueDestory(LinkQueue *Queue); //Empty queue Status QueueClear(LinkQueue *Queue); //Air judgment Status QueueEmpty(LinkQueue Queue); //Enter queue Status QueuePush(LinkQueue *Queue,ELemType Node); //Out of queue Status QueuePop(LinkQueue *Queue,ELemType *Node); //Traversal queue Status QueueTraver(LinkQueue Queue); //Generate task ELemType ProduceTask(ELemType *Node,int num); //Find length Status QueueLength(LinkQueue Queue); //Print queue Status QueuePrint(ELemType Type); //Print out team Status QueuePrint1(ELemType Type); //Get queue header element Status GetHead(LinkQueue Queue,ELemType *Node);
Implementation of basic operations
initialization
//Initialize an empty queue Status QueueInit(LinkQueue *Queue){ Queue->front = Queue->rear = (QueueNode)malloc(sizeof(QueueNode1)); if(!Queue->front){ printf("Failed to allocate space"); return FALSE; } Queue->front->next =NULL; printf("Chain team created successfully!\n"); return OK; }
Destroy queue
//Destroy queue Status QueueDestory(LinkQueue *Queue){ while(Queue->front){ Queue->rear=Queue->front->next;//Destroy it from the head of the team free(Queue->front); Queue->front = Queue->rear; } return OK; }
Clear the queue. The header node is still there
//Clear the queue. The queue head pointer is still there Status QueueClear(LinkQueue *Queue){ QueueNode p,q; Queue->rear =Queue->front;//The same as the initial state, q - > rear points to the header node p=Queue->front->next;//Start to destroy the team head element. The team head and tail remain Queue->front->next =NULL; while(p){ q = p; p = p->next; free(q); } return OK; }
Air judgment
/Is the queue empty Status QueueEmpty(LinkQueue Queue){ if(Queue.front == Queue.rear) return TRUE; else return FALSE; }
Queue number
//Get queue length Status QueueLength(LinkQueue Queue){ int count = 0; QueueNode p = Queue.front; while(Queue.rear != p){ count++; p = p->next; } printf("So far there are%d People line up!\n",count); return OK; }
Get queue header element
//Get queue header element Status GetHead(LinkQueue Queue,ELemType *Node){ QueueNode p; if(Queue.front == Queue.rear)//Team air return ERROR; p=Queue.front->next; *Node = p->data; return OK; }
Number of people inserted at the head of the team
//Insert element to tail Status QueuePush(LinkQueue *Queue,ELemType Node){ QueueNode node = (QueueNode)malloc(sizeof(QueueNode1)); if(!node){ return FALSE; } node->data = Node; node->next =NULL; Queue->rear->next =node;//The original tail next points to the new element Queue->rear =node;//Make the new element tail aligned return OK; }
Team leader element out of the team
//Team leader element out of the team Status QueuePop(LinkQueue *Queue,ELemType *Node){ QueueNode p; if(Queue->front == Queue->rear) return ERROR; p=Queue->front->next;//P points to the team head element *Node = p->data; Queue->front->next = p->next;//The successor of the head node points to the next element of the team head if(Queue->rear == p){//The head of the team is equal to the tail Queue->rear = Queue->front;//Tail pointing head node } free(p); return OK; }
Traversal task
//Traversal element Status QueueTraver(LinkQueue Queue){ QueueNode p; p = Queue.front->next; while(p){ printf("Task No.:%d\n Task duration:%d minute\n Task generation time:%s\n",p->data.TashNum,p->data.TaskTime,p->data.TaskGenerTime); p=p->next; } printf("\n"); return OK; }
Print queued tasks
//Print queue Status QueuePrint(ELemType Type){ printf("*******************************************************\n"); printf("Task No.:%d\n Task duration:%d minute\n Task generation time:%s",Type.TashNum,Type.TaskTime,Type.TaskGenerTime); printf("*******************************************************\n"); return OK; }
Print out team tasks
//Print out team Status QueuePrint1(ELemType Type){ printf("*******************************************************\n"); printf("Task No.:%d\n Task duration:%d minute\n Task generation time:%s Task start time:%s Task end time:%s",Type.TashNum,Type.TaskTime,Type.TaskGenerTime,Type.TaskStartTime,Type.TaskEndTime); printf("*******************************************************\n"); return OK; }
Generate task
ELemType ProduceTask(ELemType *Node,int num){ time_t starTime; srand((unsigned)time(0));//Random number seed //task number Node->TashNum = num; //Task duration Node->TaskTime = 1 + rand()%10; //Task generation time char *Timm = Node->TaskGenerTime; time(&starTime); Timm = ctime(&starTime); strcpy(Node->TaskGenerTime, Timm);//Pass Timm to task generation time return *Node; }
The following is the test link:
int main(){ //int i; ELemType Type; LinkQueue Queue; QueueInit(&Queue); QueueEmpty(Queue); int n; printf("Please enter the total time to generate the task(minute)\n"); scanf("%d",&n); for (int i = 1; i <= n; i++) { sleep(2); printf("task%d Join the team\n",i); ELemType Type1 = ProduceTask(&Type, i); QueuePush(&Queue, Type1); QueuePrint(Type1); } // QueueTraverse(q); time_t tm; time_t tn; printf("\n"); printf("The task generation is over, and then start processing the task!\n"); printf("\n"); while (!QueueEmpty(Queue)) { QueueLength(Queue); //Out of queue QueuePop(&Queue,&Type); char *TIME = Type.TaskStartTime; time(&tm); TIME = ctime(&tm); strcpy(Type.TaskStartTime, TIME); int TIMEE = Type.TaskTime; sleep(TIMEE); char *TIME1 = Type.TaskEndTime; time(&tn); TIME1 = ctime(&tn); strcpy(Type.TaskEndTime, TIME1); //Printed out queue QueuePrint1(Type); //Generate a task, which is composed of task number, task generation time, task duration, task start processing and task processing completion time //Given task number; The task number is generated by the for loop //Get the current time, and assign the millisecond value to the task generation time //The task duration is a random number, and a random number of 1-10 generated by the system is assigned to the task duration //Record the time when the task enters the queue and assign it to the task start time //Record the time when the task leaves the queue and assign it to the task end time } printf("End of task processing!\n"); QueueClear(&Queue); QueueDestory(&Queue); printf("The queue has been destroyed\n"); return 0; }
Operation result diagram
The full text ends!!!!!!!!!