-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTACKSUsingArrrays.cpp
More file actions
111 lines (98 loc) · 2.22 KB
/
STACKSUsingArrrays.cpp
File metadata and controls
111 lines (98 loc) · 2.22 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
#include<iostream>
#include<stack>
using namespace std;
class Stack {
int capacity;
int *arr;
int top;
public:
Stack(int c) {
this->capacity = c;
arr = new int[c];
this->top = -1;
}
void push(int data) {
if(this->top == this->capacity - 1) {
cout << "Overflow\n";
return;
}
this->top++;
this->arr[this->top] = data;
}
int pop() {
if(this->top == -1){
cout << "Underflow\n";
return INT_MIN;
}
return this->arr[this->top--];
}
int getTop() {
if(this->top == -1) {
cout << "Underflow\n";
return INT_MIN;
}
return this->arr[this->top];
}
bool isEmpty() {
return this->top == -1;
}
void display() {
int i = 0;
while(i <= top) {
cout << arr[i] << " ";
i++;
}
cout << endl;
}
int size() {
return this->top + 1;
}
bool isFull() {
return this->top == this->capacity - 1;
}
};
int main() {
Stack st(5);
st.push(10);
// int ele, data;
// while(true) {
// cout << "1.push 2.pop 3.display 4.peek 5.exit Enter your choice: ";
// cin >> ele;
// switch (ele) {
// case 1:
// cout << "Enter the element: ";
// cin >> data;
// st.push(data);
// break;
// case 2:
// st.pop();
// break;
// case 3:
// st.display();
// break;
// case 4:
// cout << st.getTop() << endl;
// break;
// case 5:
// cout << "Exiting..." << endl;
// // exit(0);
// default:
// cout << "Invalid choice. Please enter a valid option." << endl;
// break;
// }
// }
st.push(1);
st.push(2);
st.push(3);
cout<<st.getTop()<<"\n";
st.push(4);
st.push(5);
cout<< st.getTop() << "\n";
st.push(7);
st.pop();
st.pop();
cout<<st.getTop()<<"\n";
st.pop();
cout<<st.size();
return 0;
}