-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
65 lines (56 loc) · 937 Bytes
/
stack.cpp
File metadata and controls
65 lines (56 loc) · 937 Bytes
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
#include "Header.h"
#include <cstdlib>
stack::stack()
{
this->currentUse = 0;
this->memory = 1;
tab = new symbol[1];
}
//
//Always, when destructor is called size of allocated memory equal 1,
//because resize function decrease size (every time the Result function pop all elements of stack)
//
stack::~stack()
{
tab = nullptr;
}
void stack::pop()
{
currentUse--;
if ((3 * currentUse < memory) && (currentUse > 1))
{
resize(memory / 2);
}
}
bool stack::empty()
{
return (currentUse == 0);
}
void stack::resize(int newSize)
{
memory = newSize;
symbol* buffer = new symbol[newSize];
for (int i = 0; i < currentUse; i++)
{
buffer[i] = tab[i];
}
delete[] tab;
tab = buffer;
}
symbol stack::top()
{
return tab[currentUse - 1];
}
int stack::size()
{
return currentUse;
}
void stack::push(symbol newObject)
{
tab[currentUse] = newObject;
currentUse++;
if (currentUse == memory)
{
resize(memory + 10);
}
}