0% found this document useful (0 votes)
2 views3 pages

Stack Array

This document contains a C program that implements a stack using an array. It allows users to perform stack operations such as push, pop, and display, while handling overflow and underflow conditions. The program runs in a loop until the user chooses to exit, providing a simple interface for stack manipulation.

Uploaded by

maunicka
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Stack Array

This document contains a C program that implements a stack using an array. It allows users to perform stack operations such as push, pop, and display, while handling overflow and underflow conditions. The program runs in a loop until the user chooses to exit, providing a simple interface for stack manipulation.

Uploaded by

maunicka
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//stack using array

#include<stdio.h>
#include<stdlib.h>
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void display();
int main()
{
printf("\nenter the no of elements in stack");
scanf("%d",&n);
printf("\n*****Stack operations using array******");
printf("\n------------------------------\n");
while(choice!=4)
{
printf("\nchoose one from the below operations");
printf("\[Link]\[Link]\[Link]\[Link]");
printf("\nenter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
break;
default:printf("please enter valid choice");
}

}
}
void push()
{
int value;
if(top==n)
{
printf("\noverflow");
}
else
{
printf("\nenter the element");
scanf("%d",&value);
top=top+1;
stack[top]=value;
printf("\n element pushed");
}
}
void pop()
{
if(top==-1)
{
printf("\nunderflow");
}
else
{
top=top-1;
printf("\nelement poped");
}
}
void display()
{
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
if(top==-1)
{
printf("\nstack is empty");
}
}

/*output:
enter the no of elements in stack3
*****Stack operations using array******
------------------------------

choose one from the below operations


[Link]
[Link]
[Link]
[Link]
enter your choice1

enter the element10

element pushed
choose one from the below operations
[Link]
[Link]
[Link]
[Link]
enter your choice1

enter the element20

element pushed
choose one from the below operations
[Link]
[Link]
[Link]
[Link]
enter your choice1

enter the element30

element pushed
choose one from the below operations
[Link]
[Link]
[Link]
[Link]
enter your choice1

enter the element40


element pushed
choose one from the below operations
[Link]
[Link]
[Link]
[Link]
enter your choice5
please enter valid choice
choose one from the below operations
[Link]
[Link]
[Link]
[Link]
enter your choice1

overflow
choose one from the below operations
[Link]
[Link]
[Link]
[Link]
enter your choice3
40
30
20
10

choose one from the below operations


[Link]
[Link]
[Link]
[Link]
enter your choice2

element poped
choose one from the below operations
[Link]
[Link]
[Link]
[Link]
enter your choice3
30
20
10

choose one from the below operations


[Link]
[Link]
[Link]
[Link]
enter your choice4

Process returned 0 execution time : 54.117 s


Press any key to continue.*/

You might also like