forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue .c
97 lines (77 loc) · 1.7 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
#include<stdio.h>
int size, head=-1, tail=-1;
int arr[10];
void enqueue(int arr[size],int*head,int size,int*tail)
{ int element;
if(*tail>size-2)
{
printf("Enqueue not a valid expression. Overflow error.\n");
}
else
{ if(*head==-1)
{
*head=0;
}
*tail=*tail+1;
printf("Enter the element to be enqueued: ");
scanf("%d", &element);
arr[*tail]=element;
printf("The element %d has been queued successfully. \n",element);
}
}
void dequeue(int arr[size],int*head,int size,int*tail)
{ int rem;
if(*head>*tail)
{
printf("Dequeue is not a valid expression. Underflow error.\n");
}
else
{
rem=arr[*head];
*head=*head+1;
printf("The element %d has been dequeued sucessfully.\n",rem);
}
}
void display(int arr[size],int*head,int size,int*tail)
{
int i;
if(*head==-1 & *tail==-1)
{
printf("The queue is empty. \n");
}
else
{
printf("The queue is currently: \n");
for(i=*head;i<=*tail;i++)
{
printf("%d ",arr[i]);
}
}
printf("\n");
}
int main()
{ int size,op,end=0;
printf("Enter the size of the array:\n");
scanf("%d",&size);
while(end!=2)
{
printf("Enter the operation to be performed:\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
scanf("%d",&op);
switch (op)
{
case 1:
enqueue(arr,&head,size,&tail);
break;
case 2:
dequeue(arr,&head,size,&tail);
break;
case 3:
display(arr,&head,size,&tail);
break;
default:
printf("Invalid Operation.\n");
end=2;
break;
}
}
}