#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int top = -1;
int ch;
int size;
int stack[100];
int elem;
cout << "Enter size of stack: ";
cin >> size;
int set = 0;
while (ch != 4){
cout << "\nEnter 1 to push, 2 to pop and 3 to peek and 4 to exit: ";
cin >> ch;
switch(ch){
case 1:{
if (top != size - 1){
top += 1;
cout << "\nEnter the element to push: ";
cin >> elem;
stack[top] = elem;
cout <<"\n" << elem << " has been pushed";
}
else{
cout << "\nStack Overflow";
}
break;
}
case 2:{
if (top != -1){
cout << "\n" << stack[top] << " has been popped";
top = top - 1;
}
else{
cout << "\nStack Underflow";
}
break;
}
case 3: {
if (top != -1){
cout << stack[top];
}
else{
cout << "\nStack Underflow";
}
break;
}
case 4:{
cout <<"\nExiting...";
break;
}
if (ch == 4){
break;
}
getch();
}
}
#include<conio.h>
void main(){
clrscr();
int top = -1;
int ch;
int size;
int stack[100];
int elem;
cout << "Enter size of stack: ";
cin >> size;
int set = 0;
while (ch != 4){
cout << "\nEnter 1 to push, 2 to pop and 3 to peek and 4 to exit: ";
cin >> ch;
switch(ch){
case 1:{
if (top != size - 1){
top += 1;
cout << "\nEnter the element to push: ";
cin >> elem;
stack[top] = elem;
cout <<"\n" << elem << " has been pushed";
}
else{
cout << "\nStack Overflow";
}
break;
}
case 2:{
if (top != -1){
cout << "\n" << stack[top] << " has been popped";
top = top - 1;
}
else{
cout << "\nStack Underflow";
}
break;
}
case 3: {
if (top != -1){
cout << stack[top];
}
else{
cout << "\nStack Underflow";
}
break;
}
case 4:{
cout <<"\nExiting...";
break;
}
if (ch == 4){
break;
}
getch();
}
}