-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp.cpp
145 lines (117 loc) · 1.95 KB
/
cpp.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
136
137
138
139
140
141
142
143
144
145
#include <bits/stdc++.h>
using namespace std;
static const int MAX = 100000;
class mystack
{
private:
int a[MAX];
int top = 0;
public:
mystack();
~mystack();
void initialize();
bool isEmpty();
bool isFull();
void push(int);
int pop();
};
mystack::mystack()
{
}
mystack::~mystack()
{
}
void mystack::initialize() {
top = 0;
}
bool mystack::isEmpty() {
return top == 0;
}
bool mystack::isFull() {
return top >= MAX - 1;
}
void mystack::push(int val) {
if (isFull()) {
throw "Overflow";
} else {
top++;
a[top] = val;
}
}
int mystack::pop() {
if (isEmpty()) {
throw "Underflow";
} else {
top--;
return a[top + 1];
}
}
class myqueue
{
private:
int a[MAX];
int head, tail;
public:
myqueue();
~myqueue();
void initialize();
bool isEmpty();
bool isFull();
void enqueue(int);
int dequeue();
};
myqueue::myqueue()
{
}
myqueue::~myqueue()
{
}
void myqueue::initialize() {
head = tail = 0;
}
bool myqueue::isEmpty() {
return head == tail;
}
bool myqueue::isFull() {
return head == (tail + 1) % MAX;
}
void myqueue::enqueue(int val) {
if (isFull()) {
throw "Overflow";
} else {
a[tail] = val;
tail = (tail + 1) % MAX;
}
}
int myqueue::dequeue() {
if (isEmpty()) {
throw "Underflow";
} else {
head = (head + 1) % MAX;
return a[head - 1];
}
}
int main(int argc, char const *argv[])
{
mystack st;
myqueue qe;
// st.push(1);
// st.push(2);
qe.initialize();
qe.enqueue(1);
qe.enqueue(2);
qe.enqueue(3);
qe.enqueue(4);
try{
cout << qe.dequeue() << endl;
cout << qe.dequeue() << endl;
cout << qe.dequeue() << endl;
cout << qe.dequeue() << endl;
cout << qe.dequeue() << endl;
}
catch (...)
{
cout << "Exception" << endl;;
}
return 0;
}