-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
145 lines (131 loc) · 2.77 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdio.h>
#include <conio.h> // This header is non-standard. Use standard headers instead.
// Define the maximum size of the queue
#define MAX 10
// Declare the queue, front and rear variables
int data[MAX], rear, front;
// Function prototypes
void initialize();
int empty();
int full();
void insertion();
void deletion();
void print();
int main()
{
int ch;
clrscr(); // This is non-standard. Use system("cls") for Windows or a similar function for other OS.
initialize();
do
{
printf("\n ***QUEUE OPERATIONS");
printf("\n 1. Insertion");
printf("\n 2. Deletion");
printf("\n 3. Print");
printf("\n 4. EXIT");
printf("\n Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
if (!full())
{
insertion();
}
else
{
printf("\nQueue is Full!!!");
}
break;
case 2:
if (!empty())
{
deletion();
}
else
{
printf("\nQueue is Empty!!!");
}
break;
case 3:
print();
break;
case 4:
break;
default:
printf("Invalid choice");
}
} while (ch != 4);
// getch(); // This is non-standard. It is often used in older compilers. Not required in most cases.
return 0;
}
void initialize()
{
rear = front = -1;
}
int full()
{
if (rear == MAX - 1)
return 1;
else
return 0;
}
int empty()
{
if (front == -1 || front > rear) // Adjust condition to handle the case when queue is reset
return 1;
else
return 0;
}
void insertion()
{
int x;
printf("Enter data to be inserted: ");
scanf("%d", &x);
if (empty()) // Handle the case when the queue is initially empty
{
front = rear = 0;
}
else
{
rear = rear + 1;
}
data[rear] = x;
}
void deletion()
{
int x;
if (!empty())
{
x = data[front];
if (front == rear)
{
rear = front = -1; // Reset queue when it's empty after deletion
}
else
{
front = front + 1;
}
printf("\nDeleted data = %d", x);
}
else
{
printf("\nQueue is Empty!!!");
}
}
void print()
{
int i;
if (empty())
{
printf("\nQueue is Empty!!!");
}
else
{
printf("\nQueue data: ");
for (i = front; i <= rear; i++)
{
printf("%d\t", data[i]);
}
}
}