-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.c
167 lines (151 loc) · 2.81 KB
/
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
struct queue
{
int front;
int rear;
int arr[SIZE];
} queue;
void create(struct queue *q)
{
q->front = q->rear = -1;
}
int isFull(struct queue *q)
{
if (q->rear == SIZE - 1)
{
return 1;
}
else
return 0;
}
int isEmpty(struct queue *q)
{
if (q->front == -1 && q->rear == -1)
{
return 1;
}
else
return 0;
}
void insert(struct queue *q, int data)
{
if (isFull(q))
{
printf("!! Queue is Full !!");
}
else
{
if(q->front==-1)
{
q->front = 0;
}
q->rear++;
q->arr[q->rear] = data;
}
}
void delete(struct queue *q, int d)
{
if (isEmpty(q))
{
printf("!! Queue is Empty !!");
}
else
{
while (d--)
{
q->front++;
}
}
}
void displayfront(struct queue *q)
{
if (isEmpty(q))
{
printf("!! Queue is Empty !!");
}
else
{
printf("First Element of Queue is %d \n", q->arr[q->front]);
}
}
void displayrear(struct queue *q)
{
if (isEmpty(q))
{
printf("!! Queue is Empty !!");
}
else
{
printf("Last Element of Queue is %d \n", q->arr[q->rear]);
}
}
void display(struct queue *q)
{
for (int i = q->front; i <= q->rear; i++)
{
printf("%d ", q->arr[i]);
}
printf("\n");
}
int Input()
{
int num;
scanf("%d", &num);
return num;
}
int main()
{
struct queue *q;
create(q);
while (1)
{
int ch;
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
{
int n;
printf("\nEnter no. of element to Insert: ");
scanf("%d", &n);
int ip;
printf("\nEnter your Input: \n");
for (int i = 0; i < n; i++)
{
ip = Input();
insert(q, ip);
}
}
break;
case 2:
{
int d;
printf("Enter number of element to delete: ");
scanf("%d", &d);
delete (q, d);
}
break;
case 3:
{
displayfront(q);
displayrear(q);
printf("Queue is \n");
display(q);
}
break;
case 4:
exit(1);
break;
default:
break;
}
}
return 0;
}