-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeque.cpp
More file actions
155 lines (155 loc) · 2.33 KB
/
deque.cpp
File metadata and controls
155 lines (155 loc) · 2.33 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
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
146
147
148
149
150
151
152
153
154
155
#include<iostream>
#include<stdio.h>
#define MAX 10
using namespace std;
struct que
{
int arr[MAX];
int front,rear;
};
void init(struct que *q)
{
q->front=-1;
q->rear=-1;
}
void print(struct que q)
{
int i;
i=q.front;
while(i!=q.rear)
{
cout<<"\t"<<q.arr[i];
i=(i+1)%MAX;
}
cout<<"\t"<<q.arr[q.rear];
}
int isempty(struct que q)
{
return q.rear==-1?1:0;
}
int isfull(struct que q)
{
return (q.rear+1)%MAX==q.front?1:0;
}
void addf(struct que *q,int data)
{
if(isempty(*q))
{
q->front=q->rear=0;
q->arr[q->front]=data;
}
else
{
q->front=(q->front-1+MAX)%MAX;
q->arr[q->front]=data;
}
}
void addr(struct que *q,int data)
{
if(isempty(*q))
{
q->front=q->rear=0;
q->arr[q->rear]=data;
}
else
{
q->rear=(q->rear+1)%MAX;
q->arr[q->rear]=data;
}
}
int delf(struct que *q)
{
int data1;
data1=q->arr[q->front];
if(q->front==q->rear)
init(q);
else
q->front=(q->front+1)%MAX;
return data1;
}
int delr(struct que *q)
{
int data1;
data1=q->arr[q->rear];
if(q->front==q->rear)
init(q);
else
q->rear=(q->rear-1+MAX)%MAX;
return data1;
}
int main()
{
struct que q;
int data,ch;
init(&q);
while(true)
{
cout<<"\nWelcome.\nPlease enter your choice:\n1.Insert at Front\t2.Insert at Rear\n3.Delete from Front\t4.Delete from Rear\n5.Display\t\t6.Exit ";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"\nEnter data to insert at Front: ";
cin>>data;
addf(&q,data);
break;
}
case 2:
{
cout<<"\nEnter data to insert at Rear: ";
cin>>data;
addr(&q,data);
break;
}
case 3:
{
if(isempty(q))
{
cout<<"\nDequeue is empty"<<endl;
}
else
{
data=delf(&q);
cout<<"\nDeleted data is: "<<data;
}
break;
}
case 4:
{
if(isempty(q))
{
cout<<"\nDequeue is empty"<<endl;
}
else
{
data=delr(&q);
cout<<"\nDeleted data is: "<<data;
}
break;
}
case 5:
{
if(isempty(q))
{
cout<<"\nDequeue is empty"<<endl;
}
else
{
cout<<"\nDequeue elements are: "<<endl;
print(q);
}
break;
}
case 6:
{
exit(0);
}
default:
{
cout<<"Invalid choice";
}
}
}
return 0;
}