-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack_c.cpp
198 lines (182 loc) · 3.43 KB
/
stack_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <iostream>
#include <stdexcept>
#include "stack_c.h"
using namespace std;
Stack_C::Stack_C()
{
try
{
stk = new List();
}
catch (const std::bad_alloc &)
{
delete stk;
throw runtime_error("Out of Memory");
}
}
Stack_C::~Stack_C()
{
delete stk;
}
void Stack_C::push(int data)
{
stk->insert(data);
}
int Stack_C::pop()
{
if (stk->get_size() == 0)
{
throw runtime_error("Empty Stack");
}
else
{
return stk->delete_tail();
}
}
int Stack_C::get_element_from_top(int idx)
{
int s = stk->get_size();
if (idx > s - 1)
{
throw runtime_error("Index out of range");
}
else
{
Node *x = stk->get_head();
int a = s - 1 - idx;
for (int i = 0; i < a; i++)
{
x = x->next;
}
return x->next->get_value();
}
}
int Stack_C::get_element_from_bottom(int idx)
{
int s = stk->get_size();
if (idx > s - 1)
{
throw runtime_error("Index out of range");
}
else
{
Node *x = stk->get_head();
int a = idx;
for (int i = 0; i < a; i++)
{
x = x->next;
}
return x->next->get_value();
}
}
void Stack_C::print_stack(bool top_or_bottom)
{
int s = stk->get_size();
if (top_or_bottom == 1)
{
Node *x = stk->get_head();
while (x->next != nullptr)
{
x - x->next;
}
for (int i = s - 1; i >= 0; i--)
{
x = x->prev;
cout << x->get_value() << "\n";
}
}
else
{
Node *x = stk->get_head();
for (int i = 0; i < s; i++)
{
x = x->next;
cout << x->get_value() << "\n";
}
}
}
int Stack_C::add()
{
int s = stk->get_size();
if (s < 2)
{
throw runtime_error("Not Enough Arguments");
}
else
{
int a = pop(), b = pop();
int c = a + b;
push(c);
return c;
}
}
int Stack_C::subtract()
{
int s = stk->get_size();
if (s < 2)
{
throw runtime_error("Not Enough Arguments");
}
else
{
int a = pop(), b = pop();
int c = b - a;
push(c);
return c;
}
}
int Stack_C::multiply()
{
int s = stk->get_size();
if (s < 2)
{
throw runtime_error("Not Enough Arguments");
}
else
{
int a = pop(), b = pop();
int c = a * b;
push(c);
return c;
}
}
int Stack_C::divide()
{
int s = stk->get_size();
if (s < 2)
{
throw runtime_error("Not Enough Arguments");
}
else
{
if (get_element_from_top(0) == 0)
{
throw runtime_error("Divide by Zero Error");
}
else
{
int a = pop(), b = pop();
if ((a < 0 && b > 0) || (a > 0 && b < 0))
{
int c = b / a;
c -= 1;
push(c);
return c;
}
else
{
int c = b / a;
push(c);
return c;
}
}
}
}
List *Stack_C::get_stack()
{
return stk;
}
int Stack_C::get_size()
{
return stk->get_size();
}