-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy path3_Circular_Que.c
109 lines (103 loc) · 1.74 KB
/
3_Circular_Que.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
//Circular Que Operations
#include <stdio.h>
int Ar[50], front = -1, rear = -1, n;
int isFull();
int isEmpty();
void view();
//function to check whether que is empty
int isEmpty()
{
if (front == -1 && rear == -1)
return 1;
return 0;
}
//function to check whether que is full
int isFull()
{
if ((rear + 1) % n == front)
return 1;
return 0;
}
//function to insert element
int insert()
{
if (isFull())
{
printf("The Circular Queue is Full!! Overflow");
return 1;
}
else if (isEmpty())
{
rear = 0;
front = 0;
}
else
{
rear = (rear + 1) % n;
}
printf("Enter the element to be inserted:");
scanf("%d", &Ar[rear]);
view(front, rear);
}
//function to delete element
int delete ()
{
if (isEmpty())
{
printf("Underflow!!!\n");
return 1;
}
else if (front == rear)
{
printf("The element deleted is %d", Ar[front]);
front = -1;
rear = -1;
}
else
{
printf("The element deleted is %d", Ar[front]);
front = (front + 1) % n;
}
view(front, rear);
}
void view(int front, int rear)
{
int i;
printf("\nThe Current Queue is\n");
if (front > rear)
{
for (i = front; i < n; i++)
{
printf("%d ", Ar[i]);
}
for (i = 0; i <= rear; i++)
printf("%d ", Ar[i]);
}
else //case when front<rear
{
for (i = front; i <= rear; i++)
printf("%d ", Ar[i]);
}
}
void main()
{
int ch;
printf("Enter the Circular Queue size:");
scanf("%d", &n);
do
{
printf("\n---Menu---\n\n");
printf("1.Insertion\n2.Deletion\n3.Exit\n");
printf("Enter your choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert();
break;
case 2:
delete ();
break;
}
} while (ch < 3);
}