-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathqueue_array.c
More file actions
100 lines (98 loc) · 1.64 KB
/
queue_array.c
File metadata and controls
100 lines (98 loc) · 1.64 KB
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
#include<stdio.h>
#include<stdlib.h>
#define N 5
int queue [N];
int front=-1; int rear =-1;
void enqueue();
void dequeue();
void display();
void peek();
void main()
{
int choice=0;
do
{printf("enter choice 1.enqueue 2.dequeue 3.display 4.peek 5.exit\n");
scanf("%d",&choice);
switch(choice){
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
peek();
case 5:
exit(0);
default:
printf("wrong input\n");
}
} while(choice!=5);
}
void enqueue()
{
int x;
printf("enter the no u want to insert\n");
scanf("%d",&x);
if (rear== N-1)
{
printf("overflow\n");
}
else if(rear==-1 && front==-1)
{
front=0;
rear=0;
queue[rear]=x;
}
else
{
rear++;
queue[rear]=x;
}
}
void dequeue()
{
if(front==-1 && rear==-1)
{
printf("empty queue\n");
}
else if(rear==front)
{ printf("deleted item is %d\n",queue[front]);
front=rear=-1;
}
else
{
printf("the deleted element is %d\n",queue[front]);
front++;
}
}
void display()
{
int i;
printf("The elements are:\n");
if(front==-1 && rear==-1)
{
printf("empty queue");
}
else
{
for(i=front;i<rear+1;i++)
{
printf("%d\n",queue[i]);
}
}
}
void peek()
{
if(rear==-1 && front==-1)
{
printf("empty queue");
}
else
{
printf(" the front element is %d", queue[front]);
}
}