Page 130 - DCAP407_DATA_STRUCTURE
P. 130
Unit 7: Stacks
Program for Stack Implementation Using Arrays
#include <stdio.h>
#include <conio.h>
#define MAX 10
void push();
int pop();
void display();
int stack_arr[MAX];
int top= -1;
void main() /* program execution begins in the main
method */
{
int ch, pop_value;
clrscr();
do /* do …while loop displays three choices and
accepts the choice entered*/
{
printf(“\n 1. Push/n”);
printf(“\n 2. Pop/n”);
printf(“\n 3. Display/n”);
printf(“\n Enter the choice/n”);
scanf(“%d”, &ch);
switch (ch) /* switch case compares the value
entered with each case */
{
case 1: push();
break;
case 2: pop_value = pop();
break;
case 3: display();
break;
case 4: exit(1);
/* If any choice other than 1,2 or 3 is entered, the message is
displayed*/
default: printf(“\n Wrong choice”);
}
} while(1);
}
void push() /* If the value entered is 1, the push method is
called*/
{
int item;
if(top==(MAX-1)) /* if condition checks whether the stack
is full or not*/
{
printf(“\n Overflow. Stack is full”); /* If the stack is full,
the message is displayed*/
getch();
exit(0);
}
else
{
/* If the stack is not full, a message is displayed and values are
LOVELY PROFESSIONAL UNIVERSITY 123