-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathstack.cpp
More file actions
40 lines (36 loc) · 747 Bytes
/
stack.cpp
File metadata and controls
40 lines (36 loc) · 747 Bytes
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
#include <iostream>
using namespace std;
class Stack {
int top;
int* arr;
int size;
public:
Stack(int s) {
size = s;
arr = new int[s];
top = -1;
}
void push(int value) {
if (top == size - 1) {
cout << "Stack overflow\n";
return;
}
arr[++top] = value;
}
void pop() {
if (top == -1) {
cout << "Stack underflow\n";
return;
}
cout << "Popped: " << arr[top--] << endl;
}
void display() {
if (top == -1) {
cout << "Stack is empty\n";
return;
}
for (int i = 0; i <= top; i++)
cout << arr[i] << " ";
cout << endl;
}
};