-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practical_31(C++).cpp
135 lines (120 loc) · 3.11 KB
/
Practical_31(C++).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
133
134
135
/*
A double-ended queue (deque) is a linear list in which additions and deletions may be made at either end. Obtain a data
representation mapping a deque into a one-dimensional array. Write C++ program to simulate deque with functions to add
and delete elements from either end of the deque.
*/
#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 (ch != 6) {
cout << "\t\n1. Insert front"
"\t\n2. Insert rear"
"\t\n3. Delete front"
"\t\n4. Delete rear"
"\t\n5. Print"
"\t\n6. Exit";
cout << "\nEnter your choice : ";
cin >> ch;
switch (ch) {
case 1:
cout << "\nEnter data to insert front : ";
cin >> data;
addf(&q, data);
break;
case 2:
cout << "\nEnter the data to insert rear : ";
cin >> data;
addr(&q, data);
break;
case 3:
if (isempty(q))
cout << "\nDequeue is empty!!!";
else {
data = delf(&q);
cout << "\nDeleted data is : " << data;
}
break;
case 4:
if (isempty(q))
cout << "\nDequeue is empty!!!";
else {
data = delr(&q);
cout << "\nDeleted data is : " << data;
}
break;
case 5:
if (isempty(q))
cout << "\nDequeue is empty!!!";
else {
cout << "\nDequeue elements are : ";
print(q);
}
break;
}
}
return 0;
}