I. Concept of queue
A queue is a special linear table that strictly follows the "first-in-first-out" principle.
Ii. Queue Basics
Like a stack, a queue only allows you to insert and delete elements at breakpoints. The basic operations include:
(1) initqueue (& Q); // Initialize an empty queue
(2) destroyqueue (& Q); // clear the queue
(3) queueempty (& Q); // The queue is empty.
(4) queuelength (& Q); // evaluate the queue length
(5) gethead (Q, & E); // obtain the first element.
(6) enqueue (& Q, e); // Insert a new element
(7) dequeue (& Q, & E); // deletes the team Header element.
(8) queuetraverse (Q, visit (); // each element calls the visit () function.
Queue classification: chain queue and cyclic queue
C language defines the blockchain format
Typedef struct qnode {elemtype data; struct qnode * Next;} qnode, * queueptr; typedef struct {queueptr front; // head pointer, pointing to the Header element queueptr rear; // team end pointer, point to team end element} linkqueue;
You can use an ordered table to store and define cyclic queues. The Code is as follows:
# Define maxqsize 100 // longest queue length typedef struct {elemtype * base; // storage space int front; // header pointer, pointing to the queue's Header element int rear; // tail pointer, pointing to the next position of the queue element} sqqueue;
Basic queue operations
(1) The purpose of initializing queue Q is to create a queue
void InitQueue(QUEUE *Q){ Q->front = -1; Q->rear = -1;}
(2) The goal is to add a new element to the end of the team, which is equivalent to waiting in the last queue of the queue.
void EnQueue(QUEUE *Q, Elemtype elem){ if ((Q->rear+1)%MAX_QUEUE==Q->front) { exit(OVERFLOW); } else{ Q->rear = (Q->reasr + 1) % MAX_QUEUE; Q->elem[Q->rear] = elem; }}
(3) The goal of the team is to retrieve the first element and delete it at the same time so that the last element becomes the first element.
void DeQueue(QUEUE *Q, Elemtype *elem){ if (QueueEmpty(*Q)) { exit("Queue is empty."); } else { Q->front = (Q->front + 1) % MAX_QUEUE; *elem = Q->elem[Q->front]; }}
(4) obtain the first element of the queue. This element is removed from the queue header. If this element is not deleted, the queue header is still the element.
void GetFront(QUEUE *Q, Elemtype *elem){ if (QueueEmpty(*Q)) { exit("Queue is empty."); } else { *elem = Q->elem[ (Q->front + 1) % MAX_QUEUE]; }}
(5) determine whether queue Q is empty
int QueueEmpty(Queue Q){ if (Q.front==Q.rear) { return TRUE; } else { return FALSE; }}
Queue chain Storage
In C, the basic operation algorithm of chained queue is as follows:
(1) initialize queue Q. The algorithm code is as follows:
void InitQueue(QUEUE *Q){ Q->front = (LINKLIST*)malloc(sizeof(LINKLIST)); if (Q->front==NULL) { exit(ERROR); } Q->rear = Q->front;}
(2) team-up operations
void EnQueue(QUEUE *Q, Elemtype elem){ s = (LINKLIST*)malloc(sizeof(LINKLIST)); if (!s) { exit(ERROR); } s->elem = elem; s->next = NULL; Q->rear->next = s; Q->rear = s;}
(3) team-out operations
void DeQueue(QUEUE *Q, Elemtype *elem){ if (QueueEmpty(*Q)) { exit(ERROR); } else { *elem = Q->front->next->elem; s = Q->front->next; Q->front->next = s->next; Q->front = (Q->front + 1) % MAX_QUEUE; free(s); }}
(4) obtain the element of the team Header
void GetFront(QUEUE Q, Elemtype *elem){ if (QueueEmpty(Q)) { exit(ERROR); } else { *elem = Q->front->next->elem; }}
(5) determine whether queue Q is empty
void QueueEmpty(QUEUE Q){ if (Q->front==Q->rear) { return TRUE; } else { return FALSE; }}
Iii. instance Drill
Question about "add customer" Number
Code implementation:
Implementation result:
Data Structure-compliant FIFO queues (2)