-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircular queue.txt
More file actions
87 lines (84 loc) · 2.15 KB
/
Circular queue.txt
File metadata and controls
87 lines (84 loc) · 2.15 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
#include <stdio.h> //NAME-KINGSUK BASAK ROLL NO - CSE2019/027
#define MAX 4 // SETTING MAX SIZE OF ARRAY TO 4.
void Insertion(); // DEFINING ALL THE FUNCTIONS USED HERE.
void Deletion();
void Display();
int queue[MAX];
int rear = -1;
int front = -1;
main()
{
int choice; // USING A SWITCH CASE TO ASK FOR INSTRUCTIONS FROM USER.
while (1)
{
printf("\nPress (1) to insert element to queue\n");
printf("Press (2) to Delete element from queue\n");
printf("Press (3) to Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\n");
Insertion();
break;
case 2:
printf("\n");
Deletion();
break;
case 3:
exit(0);
default:
printf("\n");
printf("Invalid choice:\n");
}
}
}
void Insertion() // A FUNCTION TO INSERT NEW ELEMENT AT REAR+1 AND DISPLAY THE PRESENT CONDITION OF QUEUE.
{
int New;
if (rear == MAX - 1)
{
printf("Queue Overflow\n");
}
else
{
if (front == -1)
front = 0;
printf("Enter element for insertion = ");
scanf("%d", &New);
rear = rear + 1;
queue[rear] = New;
}
Display();
}
void Deletion() // FUNCTION TO DELETE ELEMENT FROM THE QUEUE BY FIFO AND DISPLAY THE PRESENT CONDITION OF QUEUE.
{
if (front == -1 || front > rear)
{
printf("Queue Underflow\n");
return;
}
else
{
printf("Element deleted from queue is : %d\n", queue[front]);
for (int i = 0; i <= rear - 1; i++)
{
queue[i] = queue[i + 1];
}
rear = rear - 1;
}
Display();
}
void Display() // FUNCTION TO DISPLAY THE WHOLE QUEUE IN END OF INSERTION() OR DELETION().
{
if (front == -1)
printf("Queue is empty\n");
else
{
printf("Present Queue is : ");
for (int i = 0; i <= rear; i++)
printf(" %d <-- ", queue[i]);
printf("\n");
}
}