-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_c.cpp
179 lines (166 loc) · 3.4 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
#include "stack_c.h"
#include "list.h"
#include <stdexcept>
#include <iostream>
using namespace std;
Stack_C::Stack_C()
{
try
{
stk = new List();
}
catch (...)
{
throw runtime_error("Out of Memory");
}
}
// Destructor
Stack_C::~Stack_C()
{
delete stk;
}
void Stack_C::push(int data)
{
stk->insert(data);
}
int Stack_C::pop()
{
return stk->delete_tail();
}
int Stack_C::get_element_from_top(int idx)
{
Node *se = stk->get_head();
if (stk->get_size() - idx <= 0)
{
throw runtime_error("Index out of range");
return -1;
}
else
{
for (int i = 1; i <= stk->get_size() - idx; i++)
{
se = se->next;
}
return se->get_value();
}
}
int Stack_C::get_element_from_bottom(int idx)
{
Node *se = stk->get_head();
if (stk->get_size() - idx <= 0)
{
throw runtime_error("Index out of range");
return -1;
}
else
{
while (idx)
{
se = se->next;
idx--;
}
return (se->next)->get_value();
}
}
void Stack_C::print_stack(bool top_or_bottom)
{
if (top_or_bottom)
{
Node *se = stk->get_head();
while (!((se->next)->is_sentinel_node()))
{
se = se->next;
}
while (!(se->is_sentinel_node()))
{
cout << se->get_value() << endl;
se = se->prev;
}
}
else
{
Node *sh = (stk->get_head())->next;
while (!(sh->is_sentinel_node()))
{
cout << sh->get_value() << endl;
sh = sh->next;
}
}
}
int Stack_C::add()
{
if(stk->get_size()>=2){
int a = stk->delete_tail();
int b = stk->delete_tail();
this->push(a + b);
return get_element_from_top(0);
}
else{
throw runtime_error("Not Enough Arguments");
return -1;
}
}
int Stack_C::subtract()
{
if(stk->get_size()>=2){
int a = stk->delete_tail();
int b = stk->delete_tail();
this->push(b-a);
return get_element_from_top(0);
}
else{
throw runtime_error("Not Enough Arguments");
return -1;
}
}
int Stack_C::multiply()
{
if(stk->get_size()>=2){
int a = stk->delete_tail();
int b = stk->delete_tail();
this->push(a * b);
return get_element_from_top(0);
}
else{
throw runtime_error("Not Enough Arguments");
return -1;
}
}
int Stack_C::divide()
{
if(stk->get_size()>=2){
int a, b;
a = stk->delete_tail();
b = stk->delete_tail();
if (b == 0)
{
throw runtime_error("Divide by Zero Error");
return -1;
}
int c= b/a;
if(abs(b)%abs(a)==0){
push(c);
}
else if (b*a<0){
push(c-1);
}
else{
push(c);
}
return get_element_from_top(0);
}
else{
throw runtime_error("Not Enough Arguments");
return -1;
}
}
// Get a pointer to the linked list representing the stack
List *Stack_C::get_stack()
{
return stk;
}
// Get the size of the stack
int Stack_C::get_size()
{
return stk->get_size();
}