This repository was archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathStack.cpp
More file actions
83 lines (70 loc) · 1.32 KB
/
Stack.cpp
File metadata and controls
83 lines (70 loc) · 1.32 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
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
class Stack {
int top;
public:
int a[MAX]; // Maximum size of Stack
Stack() { top = -1; }
bool push(int x);
int pop();
int peek();
bool isEmpty();
};
bool Stack::push(int x) {
// Overflow Condition
if (top >= (MAX - 1)) {
cout << "Stack Overflow";
return false;
}
else {
a[++top] = x;
return true;
}
}
int Stack::pop() {
// Underflow Conditions
if (top < 0) {
cout << "Stack Underflow";
return 0;
}
else {
int x = a[top--];
return x;
}
}
int Stack::peek() {
if (top < 0) {
cout << "Stack is Empty";
return 0;
}
else {
int x = a[top];
return x;
}
}
bool Stack::isEmpty() {
return (top < 0);
}
// Driver program to test above functions
int main() {
class Stack stk;
// Adding Item to Stack
stk.push(1);
stk.push(2);
stk.push(3);
cout<< "1, 2, 3 added to stack\n";
// Pop up Stack
cout << stk.pop() << " Popped from stack\n";
// Get the top values
cout << stk.peek() << " is the peek value\n";
// isEmpty
if (stk.isEmpty() < 0){
cout<< "Empty\n";
}
else {
cout << "Not empty\n";
}
return 0;
}