Idea: The queue is actually a linked list, but the queue also has two special nodes, one point to the team head, one point to the tail of the team. First design the data structure, as follows
struct student *struct linkqueue *struct student{ int data; struct linkqueue{ Pnode first; Pnode Rear;} queue;
1. The queue is actually a pointer to the tail of the team to move backwards, to determine whether the queues are empty or only one node situation
2. Out of the team is actually the pointer to the head of the team to move backwards
The overall code is as follows:
#include <stdio.h>#include<stdlib.h>typedefstructStudent *Pnode;typedefstructLinkqueue *Pqueue;typedefstructstudent{intdata; Pnode Next;} Node;typedefstructlinkqueue{Pnode First; Pnode Rear;} Queue Pqueue Insert (pqueue link,intnum) {Pnode p; Pqueue Q=link; P= (Pnode)malloc(sizeof(Node)); P->data=num; if(link==NULL) {printf ("add a first node \ n"); Q= (Pqueue)malloc(sizeof(queue)); Q->first=p; Q->rear=p; Q->rear->next=NULL; returnQ; } q->rear->next=p; Q->rear=p; Q->rear->next=NULL; returnQ;} Pqueue del (pqueue queue) {if(queue==NULL) {printf ("queue is empty"); returnNULL; } pqueue Q=queue; Pnode temp; Temp=q->First ; if(q->first->next!=NULL) Q->first=q->first->Next; Else{printf ("queue has only one node, delete complete \ n"); returnNULL; } Free(temp); returnq;}voidprint (pqueue link) {pnode q=link->First ; while(q!=NULL) {printf ("%d",q->data); Q=q->Next; } printf ("\ n");}intMainvoid) {Pqueue linkqueue=NULL; intflag=0, num; while(1) {printf ("choose to queue up or out: 1 for the queue, 2 for the team, and 0 for the exit \ n"); scanf ("%d",&flag); if(flag==1) {printf ("Please select a value to queue: \ n"); scanf ("%d",&num); Linkqueue=Insert (Linkqueue,num); printf ("Print queued queue: \ n"); Print (Linkqueue); } Else if(flag==2) {Linkqueue=del (linkqueue); printf ("queue after print out of team: \ n"); Print (Linkqueue); } Else Break; } printf ("Print the last queue: \ n"); Print (Linkqueue); return 0;}
Programming for queued/out-of-band operations