-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackWithDynamicArray.cpp
More file actions
117 lines (106 loc) · 2 KB
/
StackWithDynamicArray.cpp
File metadata and controls
117 lines (106 loc) · 2 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
116
117
#include <iostream>
using namespace std;
class RStack{
private:
int* a;
int capacity;
int topIndex;
bool full() const;// checking if the stack is full
void copyStack(RStack const& );//Copy of the stack
void eraseStack();//deleting the memory
void resize();///Razshirqvane na steka
void copy(int const*);//Copy the memory of the stack from another plache
public:
RStack();
~RStack();
RStack& operator=(RStack const&);
RStack(RStack const&);
bool empty() const;
void push(int const& x);
int pop();
int top() const;
};
const int INITIAL = 16;
RStack::RStack(): capacity(INITIAL),topIndex(-1) {}
RStack::~RStack()
{
eraseStack();
}
bool RStack::empty() const
{
return topIndex == -1;
}
bool RStack::full() const
{
return topIndex == capacity-1;
}
RStack& RStack::operator=(RStack const& rs)
{
if(this != &rs)
{
eraseStack();
copyStack(rs);
}
return *this;
}
void RStack::copyStack(RStack const& rs)
{
topIndex=rs.topIndex;
capacity=rs.capacity;
a = new int[capacity];
copy(rs.a);
}
void RStack::copy(int const* other)
{
for(int i=0; i<capacity; i++)
a[i] = other[i];
}
void RStack::eraseStack()
{
delete[] a;
}
RStack::RStack(RStack const& rs)
{
copyStack(rs);
}
void RStack::resize()
{
int* olda = a;
a = new int[2* capacity];
copy(olda);
capacity = capacity *2;
delete[] olda;
}
int RStack::top() const
{
if(empty())
{
cerr<<"Stack is empty!";
return 0;
}
return a[topIndex];
}
void RStack::push(int const& x)
{
if(full())
{
cerr<<"the stack is full\n";
}
a[++topIndex] = x;
}
int RStack::pop()
{
if(empty())
{
cerr<<"The stack is empty.You cannot pop!";
return 0;
}
return a[topIndex--];
}
int main()
{
RStack rs;
rs.push(5);
rs.push(6);
return 0;
}