forked from a-r-nida/HactoberFest2020-Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cqueue.c
174 lines (118 loc) · 2.26 KB
/
cqueue.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
168
169
170
171
172
173
174
#include<stdio.h>
#include<stdlib.h>
struct queue{
int size;
int f;
int r;
int *arr;
};
void createqueue(struct queue *q){
q->size=4;
q->f=-1;
q->r=-1;
q->arr=(int *)malloc(q->size * sizeof(int));
}
void enqueue(struct queue *q){
int x;
printf("enter element to insert\n");
scanf("%d",&x);
if((q->r+1)%(q->size)== q->f+1){
printf("queue is full\n");
}else{
q->r=(q->r+1)%(q->size);
q->arr[q->r]=x;
}
}
void dequeue(struct queue *q){
if(q->f==q->r){
printf("there is no element in queue");
}else{
q->f=(q->f+1)%(q->size);
printf("deleted element is %d",q->arr[q->f]);
}
}
void display(struct queue *q){
int i;
if(q->r== q->f){
printf("queue is empty");
}else{
for(i=q->f+1;i<=q->r;i++){
printf(" %d ",q->arr[i]);
}
}
}
void peek(struct queue *q){
if(q->f==q->r){
printf("no element in queue");
}else{
printf("last element in queue is %d",q->arr[q->r]);
}
}
void isempty(struct queue *q){
if(q->f==q->r){
printf("queue is empty");
}else{
printf("queue is not empty");
}
}
void isfull(struct queue *q){
if(q->r=(q->r+1)% (q->size)== q->f){
printf("queue is full");
}else {
printf("queue is not full");
}
}
int queuesize(struct queue *q){
return q->size;
}
int menu()
{
int choice;
printf("\n1.enqueue in queue\n");
printf("2. dequeue in queue\n");
printf("3.display queue\n");
printf("4.queue is empty or not\n");
printf("5.queue is full or not\n");
printf("6.size of queue\n");
printf("7.top element in queue\n");
printf("8.exit\n");
printf("enter your choice\n");
scanf("%d",&choice);
return(choice);
}
int main()
{
struct queue q;
createqueue(&q);
while(1)
{
switch(menu())
{
case 1:
enqueue(&q);
break;
case 2:
dequeue(&q);
break;
case 3:display(&q);
break;
case 4:
isempty(&q);
break;
case 5:
isfull(&q);
break;
case 6:
printf("size of queue is %d,f is %d,r is %d",queuesize(&q),q.f,q.r);
break;
case 7:
peek(&q);
break;
case 8:
exit(0);
default:
printf("enter wrong choice\n");
}
}
return 0;
}