-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
162 lines (120 loc) · 3.33 KB
/
stack.cpp
File metadata and controls
162 lines (120 loc) · 3.33 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// here is the stack implementation in c++
#include <iostream>
using namespace std;
// using linked list
struct stackNode {
int val;
stackNode* down;
stackNode(int val) : val(val), down(nullptr) {}
};
class myStack {
stackNode* top;
int count;
public:
myStack() : top(nullptr), count(0) {}
// Copy constructor (deep copy)
myStack(const myStack& other) : top(nullptr), count(0) {
if (!other.top) return;
// Reverse into a temporary stack to preserve order
myStack temp;
for (stackNode* curr = other.top; curr; curr = curr->down) {
temp.push(curr->val);
}
// Push from temp back into this stack
for (stackNode* curr = temp.top; curr; curr = curr->down) {
this->push(curr->val);
}
}
~myStack() {
while (top) {
stackNode* temp = top;
top = top->down;
delete temp;
}
}
void push(int val) {
stackNode* node = new stackNode(val);
node->down = top;
top = node;
count++;
}
void pop() {
if (!top) return; // or throw exception
stackNode* temp = top;
top = top->down;
delete temp;
count--;
}
int peek() const {
if (!top) throw runtime_error("Stack is empty");
return top->val;
}
bool isEmpty() const {
return top == nullptr;
}
int size() const {
return count;
}
// Optional: clone() method that returns a deep copy
myStack clone() const {
return myStack(*this);
}
};
int main() {
/// --------------basic ----------------------------------
/*
//intialise stack
stack<int> s;
// s.push(5);
//check empty
if(s.empty()) cout << "stack is empty";
else cout << "stack is not empty";
// push elements
s.push(5);
s.push(6);
s.push(4);
cout << endl;
// transverse the stack
stack <int> temp = s;
while(!temp.empty()){
cout << temp.top() << endl;
temp.pop();
}
cout << "top is :" << s.top() << endl;
cout << "size is :" << s.size() << endl;
// pop element
s.pop();
cout << "top is :" << s.top() << endl;
cout << "size is :" << s.size() << endl;
temp = s;
while(temp.empty()!= true){
cout << temp.top() << endl;
temp.pop();
}
*/
// -------------- custom mystack ------------------
myStack s;
cout << "Initially, is stack empty? " << (s.isEmpty() ? "Yes" : "No") << endl;
cout << "Pushing elements: 10, 20, 30" << endl;
s.push(10);
s.push(20);
s.push(30);
cout << "Top element: " << s.peek() << endl; // should print 30
cout << "Stack size: " << s.size() << endl; // should print 3
cout << "Popping top element..." << endl;
s.pop();
cout << "New top element: " << s.peek() << endl; // should print 20
cout << "Stack size: " << s.size() << endl; // should print 2
// cout << "Popping all elements: ";
// while (!s.isEmpty()) {
// cout << s.peek() << " ";
// s.pop();
// }
// cout << endl;
// cout << "Is stack empty now? " << (s.isEmpty() ? "Yes" : "No") << endl;
// Copy stack
myStack k = s.clone(); // or simply: myStack k = s;
cout << "Copy top: " << k.peek() << endl; // 20
return 0;
return 0;
}