Min in stack array by R4R Team

What is Stack ?
Stack is a linear data structure which follows a particular order in which the operations are performed.
The order may be LIFO(Last In First Out) or FILO(First In Last Out).

How find minimum element in stack:
Function:

void minimum()
{
int i,ans=stack[top];
for(i=top;i>=0;i--)
if(stack[i]< ans)
ans=stack[i];
printf("Minimum in stack is %d",ans);
}



program-

#include< stdio.h>
#include< conio.h>
#define max 50
int stack[max],top=-1;

void create()
{
char ch;
do
{
top++;
printf("Enter Numbern");
scanf("%d",&stack[top]);
printf("Continue(y/n)n");
ch=getch();
}while(ch=='y');
}

void traverse()
{
int i;
printf("Stack is :n");
for(i=top;i>=0;i--){
printf("%d ",stack[i]);
}
}

void minimum()
{
int i,ans=stack[top];
for(i=top;i>=0;i--)
if(stack[i]< ans)
ans=stack[i];
printf("Minimum in stack is %d",ans);
}

void main()
{
clrscr();
create();
traverse();
minimum();
getch();
}


output-

Enter Number
2
Continue(y/n)
Enter Number
1
Continue(y/n)
Enter Number
9
Continue(y/n)
Enter Number
3
Continue(y/n)
Enter Number
6
Continue(y/n)
Stack is :
6 3 9 1 2
Minimum in stack is 1

-In this program, we have a three function create(), traverse() and pop()
- create() function are used to create a stack
- traverse() function are used to traverse the stack
- minimum() function are used to find the minimum element of the stack.




Leave a Comment:
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!