-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAss3.cpp
More file actions
107 lines (91 loc) · 2.15 KB
/
Ass3.cpp
File metadata and controls
107 lines (91 loc) · 2.15 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
#include<bits/stdc++.h>
using namespace std;
#define MAX 100
class Stack{
int arr[MAX];
int top;
public:
Stack(){
top=-1;
}
bool isFull(){
return top==MAX -1;
}
bool isEmpty(){
return top==-1;
}
void push(int value){
if(isFull()){
cout<<"Stack is full! Cannot push "<<value<<endl;
}else{
top++;
arr[top]=value;
cout<<value<<"Pushed to stack."<<endl;
}
}
void pop(){
if(isEmpty()){
cout<<"Stack is empty ! cannot pop "<<endl;
}else{
cout<<arr[top]<<"Popped from stack ."<<endl;
top--;
}
}
void peek(){
if(isEmpty()){
cout<<"Stack is empty ."<<endl;
}else{
cout<<"Top element is ."<<arr[top]<<endl;
}
}
void display() {
if (isEmpty()) {
cout << "Stack is empty." << endl;
} else {
cout << "Stack elements: ";
for (int i = top; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};
int main(){
Stack s;
int choice, value;
do {
cout << "\n1. Push\n2. Pop\n3. Peek\n4. isEmpty\n5. isFull\n6. Display\n7. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to push: ";
cin >> value;
s.push(value);
break;
case 2:
s.pop();
break;
case 3:
s.peek();
break;
case 4:
if (s.isEmpty()) cout << "Stack is empty.\n";
else cout << "Stack is not empty.\n";
break;
case 5:
if (s.isFull()) cout << "Stack is full.\n";
else cout << "Stack is not full.\n";
break;
case 6:
s.display();
break;
case 7:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice! Try again.\n";
}
} while (choice != 7);
return 0;
}