-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.cpp
More file actions
115 lines (106 loc) · 2.38 KB
/
utility.cpp
File metadata and controls
115 lines (106 loc) · 2.38 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
#include "utils.h"
namespace MyExcel
{
Vector::Vector(int n) : data(new string[n]), capacity(n), length(0) {}
void Vector::push_back(string s)
{
if (capacity <= length)
{
string *temp = new string[capacity * 2];
for (int i = 0; i < length; i++)
{
temp[i] = data[i];
}
delete[] data;
data = temp;
capacity *= 2;
}
data[length] = s;
length++;
}
string Vector::operator[](int i) { return data[i]; }
void Vector::remove(int x)
{
for (int i = x + 1; i < length; i++)
{
data[i - 1] = data[i];
}
length--;
}
int Vector::size() { return length; }
Vector::~Vector()
{
if (data)
{
delete[] data;
}
}
Stack::Stack() : start(NULL, "") { current = &start; }
void Stack::push(string s)
{
Node *n = new Node(current, s);
current = n;
}
string Stack::pop()
{
if (current == &start)
return "";
string s = current->s;
Node *prev = current;
current = current->prev;
// Delete popped node
delete prev;
return s;
}
string Stack::peek() { return current->s; }
bool Stack::is_empty()
{
if (current == &start)
return true;
return false;
}
Stack::~Stack()
{
while (current != &start)
{
Node *prev = current;
current = current->prev;
delete prev;
}
}
NumStack::NumStack() : start(NULL, 0) { current = &start; }
void NumStack::push(double s)
{
Node *n = new Node(current, s);
current = n;
}
double NumStack::pop()
{
if (current == &start)
return 0;
double s = current->s;
Node *prev = current;
current = current->prev;
// Delete popped node
delete prev;
return s;
}
double NumStack::peek() { return current->s; }
bool NumStack::is_empty()
{
if (current == &start)
return true;
return false;
}
NumStack::~NumStack()
{
while (current != &start)
{
Node *prev = current;
current = current->prev;
delete prev;
}
}
}
int main(){
}