forked from alfaijmansuri/Data-structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue.c
More file actions
122 lines (91 loc) · 1.67 KB
/
queue.c
File metadata and controls
122 lines (91 loc) · 1.67 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 MAX 20 // you can alter size of queue by changing this
int queue[MAX];
int front = -1 , rear = -1;
void insert(void);
int delete_element(void);
int peek(void);
void display(void);
int main()
{
int option ,value;
do
{
printf("\n--- MAIN MENU ---");
printf("\n 1 : insert element");
printf("\n 2 : Delete element");
printf("\n 3 : peek");
printf("\n 4 : dipslay queue");
printf("\n 5 : exit");
printf("\n Enter your option");
scanf("%d",&value);
switch(option)
{
case 1 :
insert();
break;
case 2 :
value = delete_element();
if(value != -1)
printf("\n The element deleted from the queue is %d",value);
break;
case 3 :
value = peek();
if(value != -1 )
printf("\n The first element of the queue is %d",value);
break;
case 4 :
display();
break ;
}
}while(option != 5);
return 0;
}
void insert()
{
int number ;
printf("\n Enter the no. you want to insert in your queue");
scanf("%d",&number);
if(rear == MAX-1)
printf("\n OVERFLOW");
else if(front == -1 && rear == -1)
front=rear=0;
else
rear++;
queue[rear] = number;
}
int delete_element()
{
int value;
if(front == -1 || front > rear)
{
printf("\n UNDERFLOW");
return -1;
}
else{
value = queue[front];
front++;
//if(front > rear)
//front = rear = -1;
return value;
}
}
int peek()
{
int val;
if(front != -1 || front > rear)
printf("\n QUEUE IS EMPTY");
else
return queue[front];
}
void display()
{
int i;
if(front != -1 || front > rear)
printf("\n QUEUE IS EMPTY");
else {
for(i=front;i<=rear;i++)
printf("\t %d",queue[i]);
}
}