forked from neiljaviya/algos-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearQueue.c
More file actions
116 lines (100 loc) · 2.12 KB
/
LinearQueue.c
File metadata and controls
116 lines (100 loc) · 2.12 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdio.h>
#include <malloc.h>
#define MAX 10
int queue[MAX];
int front = -1, rear = -1;
void insert(void);
int delete_element(void);
int peek(void);
void display(void);
int main()
{
int choice, val;
do
{
printf("\n1. Insert an element into a queue ");
printf("\n2. Delete an element from a queue ");
printf("\n3. Peek an element form a queue ");
printf("\n4. Display the queue ");
printf("\n5. EXIT");
printf("\n\n\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
val = delete_element();
if(val != -1);
printf("\n The number deleted is : %d", val);
break;
case 3:
val = peek();
if(val != -1);
printf("\n The first value in queue is : %d", val);
break;
case 4:
display();
break;
}
}while(choice != 5);
return 0;
}
void insert()
{
if(rear == MAX-1)
{
printf("\nQUEUE OVERFLOW\n\n");
return;
}
if(front == -1 && rear == -1)
front = rear = 0;
else
rear++;
int num;
printf("\n Enter the element to be inserted into the queue : ");
scanf("%d", &num);
queue[rear] = num;
}
int delete_element()
{
int val;
if(front == -1 || front > rear)
{
printf("\n UNDERFLOW");
return -1;
}
else
{
val = queue[front];
front++;
if(front > rear)
front = rear = -1;
return val;
}
}
int peek()
{
if(front == -1 || front > rear)
{
printf("\n QUEUE IS EMPTY");
return -1;
}
else
{
return queue[front];
}
}
void display()
{
int i;
printf("\n");
if(front == -1 || front > rear )
printf("\n QUEUE IS EMPTY");
else
{
for(i = front; i <= rear ; i++)
printf("\t %d", queue[i]);
}
}