This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p09_2209.c
120 lines (107 loc) · 1.53 KB
/
p09_2209.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Emmanuel Jojy
// S3 CSE A
// Roll no: 53
// Prg 09/2209
// Queue Implementation
// Static Array
#include<stdio.h>
#define MAXSIZE 3
//Global variables
int Q[MAXSIZE];
int FRONT = -1;
int REAR = -1;
void queue();
void enqueue(int ITEM);
void dequeue();
void display();
void main()
{
queue();
}
void queue()
{
int ch;
while(1)
{
printf("\n\n**QUEUE OPERATIONS**");
printf("\n1. Enqueue 2. Dequeue 3. Display 4. Exit");
printf("\nChoice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
{
int ITEM;
printf("\nEnter element to enqueue: ");
scanf("%d", &ITEM);
enqueue(ITEM);
break;
}
case 2: dequeue(); break;
case 3: display(); break;
default: return;
}
}
}
void enqueue(int ITEM)
{
if(REAR == MAXSIZE - 1)
{
printf("\nQUEUE IS FULL!");
return;
}
if(FRONT == -1 && REAR == -1)
{
FRONT++;
}
REAR++;
Q[REAR] = ITEM;
}
void dequeue()
{
if(FRONT == -1 && REAR == -1)
{
printf("\nQUEUE IS EMPTY!");
return;
}
int ITEM = Q[FRONT];
printf("\nDequeued %d Successfully", ITEM);
if(FRONT == REAR)
{
FRONT = -1;
REAR = -1;
}
else
{
FRONT++;
}
//return ITEM;
}
void display()
{
if(FRONT == -1 && REAR == -1)
{
printf("\nQUEUE IS EMPTY!");
return;
}
int i;
printf("\nCURRENT QUEUE:\n");
for(i = 0; i < MAXSIZE; i++)
{
printf("[%d]\t", i);
}
printf("\n");
for(i = 0; i < FRONT; i++)
{
printf("***\t");
}
for(i = FRONT; i <= REAR; i++)
{
printf("%d\t", Q[i]);
}
for(i = REAR + 1; i < MAXSIZE; i++)
{
printf("***\t");
}
printf("\n");
}