-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cpp
More file actions
91 lines (84 loc) · 1.51 KB
/
Copy pathStack.cpp
File metadata and controls
91 lines (84 loc) · 1.51 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
#include<iostream>
using namespace std;
class Stack
{
private:
int size;
int *st;
int tos;
static int count;
public:
friend void viewContent(Stack s);
Stack (Stack &z);
Stack(int s = 10)
{
tos = 0;
size = s;
count++;
st = new int [size];
}
~Stack()
{
delete [] st;
count--;
}
void push(int n)
{
if(tos == size)
{
cout << "Stack is full" << endl;
}
else{
st[tos] = n;
tos++;
}
};
int pop ()
{
int retVal;
if(tos == 0)
{
cout << "Stack is empty" << endl;
retVal = -1;
}
else{
tos--;
retVal = st[tos];
}
return retVal;
};
static int getCounter()
{
return count;
}
};
int Stack::count = 0;
// copy constructor implementation
Stack :: Stack (Stack &x)
{
tos = x.tos;
size = x.size;
st = new int [size]; // new allocation instead of copying to avoid changing the main array content
for(int i = 0; i < tos; i++)
{
st[i] = x.st[i];
}
count++;
};
void viewContent(Stack s)
{
int t = s.tos;
while(t != 0)
{
cout << s.st[--t] << endl;
}
}
int main()
{
Stack s1(5);
s1.push(5);
s1.push(7);
viewContent(s1);
cout << s1.pop() << " " << s1.pop() << " " << Stack::getCounter() << endl;
return 0;
}