-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
64 lines (53 loc) · 1.04 KB
/
stack.cpp
File metadata and controls
64 lines (53 loc) · 1.04 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
#include <iostream>
using namespace std;
class Stack {
public:
int *a;
int top;
int size;
Stack(int data) {
a = new int[data];
top = -1;
size = data;
}
void push(int b) {
if (top >= size - 1) {
cout << "Stack is full" << endl;
} else {
top++;
a[top] = b;
cout << b << " pushed to stack." << endl;
}
}
int peek() {
if (isempty()) {
cout << "Stack is empty" << endl;
return -1;
}
return a[top];
}
void pop() {
if (isempty()) {
cout << "Stack is empty nothing to pop." << endl;
} else {
cout << a[top] << " popped from stack" << endl;
top--;
}
}
bool isempty() {
return top == -1;
}
};
int main() {
Stack s(3);
s.push(4);
s.push(5);
s.push(9);
s.push(20);
cout << "Top element: " << s.peek() << endl;
s.pop();
s.pop();
s.pop();
s.pop();
return 0;
}