You are on page 1of 3

#include <iostream.

h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100
int stack[MAX];
int top;

void push(int stack[], int val, int &top);
int pop(int stack[], int &top);
void show_Stack(int stack[], int top);
void main()
{
int choice, val;
char opt = 'Y';
top = -1;
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Stack";
cout << "\n\t2. Deletion from Stack";
cout << "\n\t3. Traverse of Stack";
cout << "\n\t4. Exit from Menu";
cout << "\n\nEnter your choice from above -> ";
cin >> choice;
switch (choice)
{
case 1:
do
{
cout << "Enter the value to be added in the stack ";
cin >> val;
push(stack, val, top);
cout << "\nDo you want to add more elements <Y/N> ? ";
cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 2:
opt = 'Y';
do
{
val = pop(stack, top);
if (val != -1)
cout << "Value deleted from statck is " << val;
cout << "\nDo you want to delete more elements <Y/N> ? ";
cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 3:
show_Stack(stack, top);
break;
case 4:
exit(0);
}
}
while (choice != 4);
}

void push(int stack[], int val, int &top)
{
if (top == MAX - 1)
{
cout << "Stack Full ";
}
else
{
top = top + 1;
stack[top] = val;
}
}

int pop(int stack[], int &top)
{
int value;
if (top < 0)
{
cout << "Stack Empty ";
value = -1;
}
else
{
value = stack[top];
top = top - 1;
}
return (value);
}

void show_Stack(int stack[], int top)
{
int i;
if (top < 0)
{
cout << "Stack Empty";
return;
}
i = top;
clrscr();
cout << "The values are ";
do
{
cout << "\n" << stack[i];
i = i - 1;
}while(i >= 0);
}

OUTPUT


Main Menu
1. Addition of Stack
2. Deletion from Stack
3. Traverse of Stack
4. Exit from Menu

Enter your choice from above -> 1
Enter the value to be added in the stack 2

Do you want to add more elements <Y/N> ? y
Enter the value to be added in the stack 4

Do you want to add more elements <Y/N> ? n
Main Menu
1. Addition of Stack
2. Deletion from Stack
3. Traverse of Stack
4. Exit from Menu

Enter your choice from above -> 3
The values are
4
2
Main Menu
1. Addition of Stack
2. Deletion from Stack
3. Traverse of Stack
4. Exit from Menu

Enter your choice from above -> 2
Value deleted from statck is 4
Do you want to delete more elements <Y/N> ? n

Main Menu
1. Addition of Stack
2. Deletion from Stack
3. Traverse of Stack
4. Exit from Menu

Enter your choice from above -> 4

You might also like