0% found this document useful (0 votes)
1K views3 pages

Aim: Write A Menu Driven Program To Implement Stack Using Array

This document describes an experiment to implement a stack using arrays in C. It includes functions to push, pop, peek and display elements in the stack. The main program contains a menu to call these functions and manipulate the stack. It initializes the stack array and top pointer, then enters a loop to accept user input and call the corresponding function to push, pop, peek or display the stack until the user chooses to exit.

Uploaded by

enggeng7
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)
1K views3 pages

Aim: Write A Menu Driven Program To Implement Stack Using Array

This document describes an experiment to implement a stack using arrays in C. It includes functions to push, pop, peek and display elements in the stack. The main program contains a menu to call these functions and manipulate the stack. It initializes the stack array and top pointer, then enters a loop to accept user input and call the corresponding function to push, pop, peek or display the stack until the user chooses to exit.

Uploaded by

enggeng7
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/ 3

EXPERIMENT NO: 4 STACK USING ARRAYS

Aim: Write a menu driven program to implement stack using array


Program:
#include<stdio.h>
#include<conio.h>
#define max 10
int st[max],top=-1;
void push(int st[],int val);
int pop(int st[]);
int peek(int st[]);
void display(int st[]);
int main()
{
int val,option;
clrscr();
do
{
printf("\n**MAIN MENU**");
printf("\n1.push\n2.pop\n3.peek\n4.display\n5.exit");
printf("\nEnter your option:");
scanf("%d",&option);
switch(option)
{
case 1:printf("\nEnter the number to be pushed on stack");
scanf("%d",&val);
push(st,val);
break;
case 2:val=pop(st);
if(val!=-1)
printf("\nThe value deleted from stack is %d",val);
break;
case 3:val=peek(st);
if(val!=-1)
printf("\nThe value stored at top of stack is %d",val);
break;
case 4:display(st);
break;
}
}while(option!=5);
getch();
return 0;
}
void push(int st[],int val)
{

if(top==max-1)
{
printf("\nstack overflow");
}
else
{
top++;
st[top]=val;
}
}
int pop(int st[])
{
int val;
if(top==-1)
{
printf("\nstack underflow");
return -1;
}
else
{
val=st[top];
top--;
return val;
}
}
void display(int st[])
{
int i;
if(top==-1)
printf("\nstack is empty");
else
{
for(i=top;i>=0;i--)
printf("%d\t",st[i]);
}
}
int peek(int st[])
{
if(top==-1)
{
printf("\nstack is empty");
return -1;
}
else
return(st[top]);
}

OUTPUT

You might also like