-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS_Queue1.c
81 lines (73 loc) · 1.32 KB
/
DS_Queue1.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
#include<stdio.h>
#include<stdlib.h>
# define N 20 //defining the size of queue
int s[N],top=-1;
int pop()
{
return s[top--];
}
void push(int x)
{
if(top == N-1)
printf("STACK IS FULL");
else
{
top++;
s[top] = x;
}
}
void enqueue(int x)//function to insert element in the queue using recursive stack call
{
push(x);
}
void print()//function to print elements of a queue
{
int i;
for(i=0;i<=top;i++)
printf("\n%d",s[i]);
}
int dequeue()//function to dequeue element from a queue using recursive stack call
{
int data,res;
if(top == -1)
printf("QUEUE IS EMPTY");
else if(top == 0)
return pop();
data = pop();
res = dequeue();
push(data);
return res;
}
int main()
{
int choice,n,i,data,t;
printf("ENTER YOUR CHOICE:- ");
do{
printf("\n\n1 TO INSERT DATA IN QUEUE\n2 TO SHOW DATA IN QUEUE \n3 TO DELETE DATA FROM QUEUE\n0 TO EXIT\n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("\nENTER THE NUMBER OF ELEMENTS:- ");
scanf("%d",&n);
printf("\nENTER THE DATA:- \n");
i=0;
while(i<n){
scanf("%d",&data);
enqueue(data);
i++;
}
break;
case 2:
print();
break;
case 3:
t = dequeue();
printf("DEQUEUED ELEMENT:- %d",t);
break;
case 0:
break;
default:
printf("\nINVALID CHOICE!!!!");
}
}while(choice!=0);
}