-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcircuklar_queue_using_array.c
103 lines (96 loc) · 1.84 KB
/
circuklar_queue_using_array.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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define max 5
struct queue
{
int front;
int rear;
int arr[max];
};
void enque(struct queue *en, int x)
{
if ((en->rear+1)%max == en->front )
{
printf("the queue is full\n");
return;
}
else if (en->front == -1 && en->rear == -1)
{
en->front = 0;
en->rear = 0;
en->arr[en->rear] = x;
}
else
{
en->rear= (en->rear+1)%max;
en->arr[(en->rear)] = x;
}
}
int deque(struct queue *en)
{
int temp;
if (en->front < 0)
{
printf("the queue is empty\n");
return -1;
}
else if (en->rear == en->front)
{
temp = en->arr[en->front];
en->front = en->rear = -1;
return temp;
}
else
{
temp = en->arr[en->front];
en->front=(en->front+1)%max;
return temp;
}
}
void display(struct queue *q)
{
if (q->front < 0)
{
printf("the queue is empty\n");
}
else
{
int i=q->front;
printf("the elements in queue are\n");
while(i!=q->rear)
{
printf("%d",q->arr[i]);
i=(i+1)%max;
}
printf("%d",q->arr[i]);
}
}
int main()
{
struct queue s;
s.front=-1;
s.rear=-1;
int choice,n;
do
{
printf("enter your choice\n 1 FOR enqueue\n 2 FOR dequeue \n 3 FOR DISPLAY\n 0 FOR EXIT\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("enter the no. you want to push\n ");
scanf("%d",&n);
enque(&s,n);
break;
case 2:
n=deque(&s);
printf("the deleted no. is %d\n",n);
break;
case 3:
display(&s);
break;
}
} while (choice);
return 0;
}