-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDqueue.cpp
132 lines (121 loc) Β· 2.01 KB
/
Dqueue.cpp
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
#include<stdio.h>
#include<stdlib.h>
#define max 5
typedef struct queue
{
char data[max];
int front,rear;
}queue;
int isempty(queue *q)
{
if(q->front==-1)
return 1;
else
return 0;
}
int isfull(queue *q)
{
if((q->front == q->rear + 1) || (q->front == 0 && q->rear == max - 1))
return 1;
else
return 0;
}
void enquecircular(queue *q,char x)
{
if(isfull(q))
{
printf("\nqueue overflow");
return;
}
else
{
if (q->front == -1)
q->front = 0;
q->rear = (q->rear + 1) % max;
q->data[q->rear] = x;
}
}
char dequeuecircular(queue *q)
{
char x;
if(isempty(q))
{
printf("\nqueue underflow");
return -1;
}
else {
x = q->data[q->front];
if (q->front == q->rear) {
q->front = -1;
q->rear = -1;
}
else {
q->front = (q->front + 1) % max;
}
return (x);
}
}
void displaycircular(queue *q)
{
int i;
if (isempty(q))
printf(" \n Empty Queue\n");
else {
for (i = q->front; i != q->rear; i = (i + 1) % max) {
printf("%c ", q->data[i]);
}
printf("%c ", q->data[i]);
}
}
void count(queue *q)
{
int count=0,i;
for(i=q->front;i<=q->rear;i++)
{
count++;
}
printf("Number of Element in queue is :%d\n",count);
}
int main()
{
queue *q;
q=(queue*)malloc(sizeof(queue));
int op;
char item,ch;
q->front=-1;
q->rear=-1;
while(1)
{
printf("\n1.INSERT\n2.DELETE\n3.COUNT\n4.DISPLAY\n5.EXIT\n");
printf("Enter option : ");
scanf("%d",&op);
switch(op)
{
case 1:
scanf("%c",&item);
printf("Enter item to insert : ");
scanf("%c",&item);
enquecircular(q,item);
break;
case 2:
printf("The deleted elements are:-\n");
ch=dequeuecircular(q);
if(ch!=-1)
{
printf("\nDeleted item = %c",ch);
}
break;
case 3:
printf("The number of elements are:-\n");
count(q);
break;
case 4:
printf("The Elements are:-\n");
displaycircular(q);
break;
case 5:exit(0);
default:printf("Invalid option\n");
}
}
return 0;
}