-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion_01.cpp
More file actions
72 lines (67 loc) · 1.82 KB
/
Copy pathQuestion_01.cpp
File metadata and controls
72 lines (67 loc) · 1.82 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
#include <iostream>
using namespace std;
#define MAXIMUM 50
class Stack {
private:
int arr[MAXIMUM];
int top;
public:
Stack() {
top = -1;
}
bool isEmpty() {
return (top == -1);
}
bool isFull() {
return (top == MAXIMUM - 1);
}
void push(int value) {
if (isFull()) {
cout << "Stack Overflow" << endl;
} else {
arr[++top] = value;
}
}
int pop() {
if (isEmpty()) {
cout << "Stack Underflow" << endl;
return -1;
} else {
return arr[top--];
}
}
int stackTop() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return -1;
} else {
return arr[top];
}
}
void display() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
} else {
cout << "Elements in the stack are: ";
for (int i = 0; i <= top; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};
int main() {
Stack stac;
stac.push(20);
stac.push(30);
stac.push(40);
stac.push(60);
stac.display();
cout << "Top of the stack is : " << stac.stackTop() << endl;
stac.pop();
cout << "After the pop is: " << stac.stackTop() << endl;
stac.display();
cout << "Is the stack empty? " << (stac.isEmpty() ? "Yes" : "No") << endl;
cout << "Is the stack full? " << (stac.isFull() ? "Yes" : "No") << endl;//it's not full unless the top is 49
return 0;
}