-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_own.cpp
More file actions
120 lines (99 loc) · 2.15 KB
/
Copy pathstack_own.cpp
File metadata and controls
120 lines (99 loc) · 2.15 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
118
119
120
#include<iostream>
using namespace std;
class StackInt
{
private:
int *data;
int size_t;
int capacity;
int top;
public:
StackInt(int cap):size_t(0),capacity(cap)
{
top = -1;
data = new int[capacity];
}
~StackInt() {
delete []data;
}
StackInt(const StackInt& rhs)
{
cout << "Copy constructor called" << endl;
this->size_t = rhs.size_t;
this->capacity = rhs.capacity;
this->top = rhs.top;
this->data = new int[this->size_t];
for(int i = 0 ; i <= top; i++)
{
data[i] = rhs.data[i];
}
}
int size() const { return size_t; }
bool empty() const { return (top == -1); }
bool full() const { return top == (capacity - 1); }
void push_back(const int& val)
{
if(full())
{
cout << "Stack is full" << endl;
}
else
{
top++;
data[top] = val;
size_t++;
}
}
void pop_back()
{
if(empty())
{
cout << "Stack is empty" << endl;
return;
}
else
{
top--;
size_t--;
}
}
int topValue() const { return data[top]; }
void displayStack() const {
for(int i = 0; i <= top; i++)
{
cout << data[i] << " ";
}
cout << endl;
}
void display(StackInt obj)
{
while(!obj.empty())
{
cout << obj.topValue() << " ";
obj.pop_back();
}
cout << endl;
}
};
int main()
{
StackInt q(5);
q.pop_back();
q.push_back(6);
q.push_back(7);
q.push_back(8);
q.push_back(9);
q.push_back(10);
cout << q.size() << endl;
q.displayStack();
q.push_back(11);
q.displayStack();
cout << q.topValue() << endl;
q.pop_back();
q.pop_back();
q.displayStack();
cout << q.size() << endl;
cout << q.topValue() << endl;
q.display(q);
return 0;
}