-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQUEUE1.cpp
More file actions
65 lines (58 loc) · 1.29 KB
/
QUEUE1.cpp
File metadata and controls
65 lines (58 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdbool.h>
#define Max_customers 5
typedef struct {
int data[Max_customers];
int front;
int rear;
int count;
}CustomerQueue;
void TaoQueue(CustomerQueue *q){
q->front =0;
q->rear=-1;
q->count=0;
}
bool isEmpty(CustomerQueue *q){
return q->count==0;
}
bool isFull(CustomerQueue *q){
return q->count==Max_customers;
}
void enqueue(CustomerQueue *q, int customerID){
if(isFull(q)){
printf("FULL,customer ID %d please wait.\n",customerID);
return;
}
q->rear=(q->rear+1)%Max_customers;
q->data[q->rear]=customerID;
q->count++;
printf("ENQUEUE:%d.\n",customerID);
}
int dequeue(CustomerQueue *q){
if(isEmpty(q)){
printf("Empty.\n");
return -1;
}
int customerID = q->data[q->front];
q->front=(q->front+1)%Max_customers;
q->count--;
return customerID;
}
int main(){
CustomerQueue q;
TaoQueue(&q);
printf("---------------------------------------\n");
enqueue(&q,101);
enqueue(&q,102);
enqueue(&q,103);
printf("DEQUEUE:%d\n",dequeue(&q));
printf("DEQUEUE:%d\n",dequeue(&q));
enqueue(&q,104);
enqueue(&q,105);
enqueue(&q,106);
printf("DEQUEUE:%d\n",dequeue(&q));
printf("DEQUEUE:%d\n",dequeue(&q));
printf("DEQUEUE:%d\n",dequeue(&q));
printf("DEQUEUE:%d\n",dequeue(&q));
return 0;
}