0% found this document useful (0 votes)
0 views

stack using array

The document contains a C program that implements a stack data structure with operations for pushing, popping, peeking, and displaying elements. It defines a stack of fixed size and provides a menu-driven interface for user interaction. The program handles basic error checking for stack overflow and underflow conditions.

Uploaded by

mumebepi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

stack using array

The document contains a C program that implements a stack data structure with operations for pushing, popping, peeking, and displaying elements. It defines a stack of fixed size and provides a menu-driven interface for user interaction. The program handles basic error checking for stack overflow and underflow conditions.

Uploaded by

mumebepi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<stdio.

h>
#define size 5
int stack[size];
int top=0;
int push(int n);
int pop();
int peek();
int display();
int main();
int push(int n)
{
stack[top]=n;
}
int pop()
{
if(top==0)
printf("Stack is empty");
else
top--;
}
int peek()
{
if(top==0)
printf("Stack is empty");
else
printf("The top element is %d",stack[top-1]);
}
int display()
{
int i;
if(top==0)
printf("Stack is empty");
else
{
for(i=top-1;i>=0;i--)
printf("%d\t",stack[i]);
}
}
int main()
{
int option,n;
while(1)
{
printf("\n1.Push\n2.Pop\n3.Peek\n4.Display\n5.Exit");
printf("\n\nPlease enter you choice of operation : ");
scanf("%d",&option);
switch(option)
{
case 1:
if(top==size)
printf("Stack is full");
else
{
printf("Enter a number to add to the stack : ");
scanf("%d",&n);
push(n);
top++;
}
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
return 0;
default:
printf("Invalid choice entered");
}
}
}

You might also like