New Microsoft Word Document
New Microsoft Word Document
#include<string> }
using namespace std; }
class Stack {
private: int count() {
int top; return (top + 1);
int arr[5]; }
public:
Stack() { int peek(int pos) {
top = -1; if (isEmpty()) {
for (int i = 0; i < 5; i++) { cout << "stack underflow" << endl;
arr[i] = 0; return 0;
} } else {
} return arr[pos];
}
bool isEmpty() { }
if (top == -1)
return true; void change(int pos, int val) {
else arr[pos] = val;
return false; cout << "value changed at location " << pos
} << endl;
bool isFull() { }
if (top == 4)
return true; void display() {
else cout << "All values in the Stack are " <<
return false; endl;
} for (int i = 4; i >= 0; i--) {
cout << arr[i] << endl;
void push(int val) { }
if (isFull()) { }
cout << "stack overflow" << endl; };
} else {
top++; // 1 int main() {
arr[top] = val; Stack s1;
} int option, postion, value;
}
do {
int pop() { cout << "What operation do you want to
if (isEmpty()) { perform? Select Option number. Enter 0 to
cout << "stack underflow" << endl; exit." << endl;
return 0; cout << "1. Push()" << endl;
} else { cout << "2. Pop()" << endl;
int popValue = arr[top]; cout << "3. isEmpty()" << endl;
arr[top] = 0; cout << "4. isFull()" << endl;
top--; cout << "5. peek()" << endl;
cout << "6. count()" << endl; cout << "Change Function Called - " <<
cout << "7. change()" << endl; endl;
cout << "8. display()" << endl; cout << "Enter position of item you want
cout << "9. Clear Screen" << endl << endl; to change : ";
cin >> postion;
cin >> option; cout << endl;
switch (option) { cout << "Enter value of item you want to
case 0: change : ";
break; cin >> value;
case 1: s1.change(postion, value);
cout << "Enter an item to push in the break;
stack" << endl; case 8:
cin >> value; cout << "Display Function Called - " <<
s1.push(value); endl;
break; s1.display();
case 2: break;
cout << "Pop Function Called - Poped case 9:
Value: " << s1.pop() << endl; system("cls");
break; break;
case 3: default:
if (s1.isEmpty()) cout << "Enter Proper Option number " <<
cout << "Stack is Empty" << endl; endl;
else }
cout << "Stack is not Empty" << endl;
break; } while (option != 0);
case 4:
if (s1.isFull()) return 0;
cout << "Stack is Full" << endl; }
else
cout << "Stack is not Full" << endl;
break;
case 5:
cout << "Enter position of item you want
to peek: " << endl;
cin >> postion;
cout << "Peek Function Called - Value at
position " << postion << " is " <<
s1.peek(postion) << endl;
break;
case 6:
cout << "Count Function Called - Number
of Items in the Stack are: " << s1.count() <<
endl;
break;
case 7: