CycQueue.h
/*queue:first in First out (FIFO) avoids false overflow: Use loop queue. */#define QUEUEMAX 20//1. Define the queue structure typedef struct {DATA data[queuemax];//queue array int head;//team head int tail;//team tail}cycqueue;//2. Initialize Queue Cycqueue *cycqueueinit () {Cycqueue *q;if (q= (Cycqueue)) malloc (sizeof (Cycqueue))) {Q->head = 0;//Set Team Header q-> Tail = 0;//Set the end of the queue return q;} Elsereturn NULL;} void Cycqueuefree (Cycqueue *q)//release Queue {if (q!=null) free (q);} 3. Get the queue status int cycqueueisempty (Cycqueue *q)//Determine if the queue is empty {return (q->head==q->tail);} int Cycqueueisfull (Cycqueue *q)//Determine if the queue is full {return (q->tail+1)%queuemax==q->head);} 4. Enqueue operation int Cycqueuein (Cycqueue *q,data DATA) {if ((q->tail+1)%queuemax = = Q->head) {printf ("queue is full \ n"); return 0;} Else{q->tail = (q->tail+1)%queuemax;//queue tail ordinal q->data[q->tail] = Data;return 1;}} 5. Outbound Operation Data *cycqueueout (Cycqueue *q) {if (q->head==q->tail) {printf ("queue is empty \ n"); return NULL;} Else{q->head = (q->head+1)%queuemax;return & (Q->data[q->head]);}} 6. Get Queue Length int cycqueuelen (Cycqueue *q) {int n;n=q->tail-q->head;if (n<0) N=queuemax+n;return N;} 7. Get Data *cycqueuepeek (Cycqueue *q) {if (q->head==q->tail) {printf ("queue is empty \ n") for the first position in the queue; return NULL;} Else{return & (q->data[(q->head+1)%queuemax]);}}
Bankqueue.c
Analog Bank customer automatic arranging # include <stdio.h> #include <stdlib.h> #include <time.h>typedef struct {int num;//customer number Long time;//Enter the queue time}data; #include "CycQueue.h" int num;//Save the customer's ordinal void Add (Cycqueue *q)//Add customer arrangement {DATA data;if (! Cycqueueisfull (q)) {data.num = ++num;data.time = Time (NULL); Cycqueuein (q,data);//Queue}elseprintf ("\ \ \ \ \ \ \ \ \ \") void Next (Cycqueue *q)//Notify next customer to prepare {DATA *data;if (! Cycqueueisempty (q)) {data = Cycqueueout (q);//out-of-line printf ("\ n please the customer with number%d" \ n ", data->num);} if (! Cycqueueisempty (q)) {data = Cycqueuepeek (q);//Take the team first element printf ("Please be prepared for the customer with the number%d, immediately after the business for you \ n", data->num);} int main () {cycqueue *queue;int select;num = 0;queue = Cycqueueinit ();d o{printf ("\ n Please select specific action: \ n");p rintf ("1. New to Customer \ n"); printf ("2. Next customer \ \");p rintf ("0. exit \ n"); Fflush (stdin); scanf ("%d", &select); switch (select) {case 1:add (queue); printf ("\ n now there are%d customers waiting! \ n ", Cycqueuelen (queue)), Break;case 2:next (queue);p rintf (" \ n now there are%d customers waiting! \ n ", Cycqueuelen (queue)); Break;case 0:break;}} while (select!=0); Cycqueuefree (queue); return 0;}
Bank automatic arranging simulation in the queue