-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path29-linear-queue.c
131 lines (117 loc) · 2.69 KB
/
29-linear-queue.c
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <stdlib.h>
#define QUEUESIZE 5
#define TRUE 1
#define FALSE 0
struct queue
{
int front;
int rear;
int items[QUEUESIZE];
};
typedef struct queue QUEUE;
void enqueue(QUEUE *);
void dequeue(QUEUE *);
void display(QUEUE *);
int full(QUEUE *);
int empty(QUEUE *);
int main()
{
QUEUE q;
q.front = 0;
q.rear = -1;
int choice;
while(1){
printf("MENU\n");
printf("1-Enqueue\n");
printf("2-Dequeue\n");
printf("3-Display\n");
printf("4-Exit\n");
printf("\nEnter your choice\n ");
scanf("%d", &choice);
switch(choice){
case 1: enqueue(&q);
break;
case 2: dequeue(&q);
break;
case 3: display(&q);
break;
case 4: printf("Terminating\n");
exit(0);
}
}
return 0;
}
/// Function Name: full
/// Description: checks if rear end has reached max position
/// Input Param: Pointer to queue
/// Return type: TRUE if queue full, FALSE otherwise
int full(QUEUE *q)
{
if(q->rear == QUEUESIZE - 1)
return TRUE;
else
return FALSE;
}
/// Function Name: enqueue
/// Description: enqueue an item inside queue
/// Input Param: Pointer to queue
/// Return type: void
void enqueue(QUEUE *q)
{
if(full(q)){
printf("Queue full\n");
return;
}
int x;
printf("Enter the enqueue item\n");
scanf("%d", &x);
q->rear++;
q->items[q->rear] = x;
}
/// Function Name: empty
/// Description:
/// item f r
/// -------------------------------------
/// initial 0 -1
/// one insertion/deletion 1 0
/// ...
/// n insertions/deletions n n-1`
/// Input Param: Pointer to queue
/// Return type: TRUE if queue empty, FALSE otherwise
int empty(QUEUE *q)
{
if(q->front > q->rear)
return TRUE;
else
return FALSE;
}
/// Function Name: dequeue
/// Description: dequeue an item from queue
/// Input Param: Pointer to queue
/// Return type: void
void dequeue(QUEUE *q)
{
if(empty(q)){
printf("Empty queue\n");
return;
}
int x;
x = q->items[q->front];
printf("Dequeued Item is %d\n", x);
q->front++;
}
/// Function Name: display
/// Description: display the items from queue
/// Input Param: Pointer to queue
/// Return type: void
void display(QUEUE *q)
{
if(empty(q)){
printf("Empty Queue\n");
return;
}
int i;
for(i = q->front; i<= q->rear; i++)
printf("%d\n", q->items[i]);
}