0% found this document useful (0 votes)
49 views2 pages

Stack Using Array@

This document describes a C program that implements a stack using an array. It defines functions to push values onto the stack, pop values off the stack, and display the stack. The main function initializes the stack array and top index, then presents a menu to allow the user to push, pop, display, or exit. It calls the appropriate functions to perform the selected operation, then loops back to the menu until the user chooses to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views2 pages

Stack Using Array@

This document describes a C program that implements a stack using an array. It defines functions to push values onto the stack, pop values off the stack, and display the stack. The main function initializes the stack array and top index, then presents a menu to allow the user to push, pop, display, or exit. It calls the appropriate functions to perform the selected operation, then loops back to the menu until the user chooses to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

STACK USING ARRAY

#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. Push";
cout << "\n\t2. Pop";
cout << "\n\t3. Display";
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;
}
clrscr();
cout << "The values are ";
for(i=top;i>=0;i--)
cout << "\n" << stack[i];
}

You might also like