program:-
#include<stdio.h>
struct stack
{
int top;
int capacity;
int *array;
};
// function for create stack
struct stack* creatstack(int cap)
{
struct stack *stack;
stack=(struct stack*)malloc(sizeof(struct stack));
stack->top=-1;
stack->capacity=cap;
stack->array=(int *)malloc(sizeof(int)*stack->capacity);
return(stack);
}
// function for check stack is full or not
int isfull(struct stack* stack)
{
if(stack->top==stack->capacity-1)
return(0);
else
return(1);
}
// function for check stack is empty or not
int isempty(struct stack *stack)
{
if(stack->top==-1)
return(0);
else
return(1);
}
// function for push element
void push(struct stack *stack,int val)
{
if(isfull(stack))
{
stack->top++;
stack->array[stack->top]=val;
}
else
printf("Stack is Full");
}
// function for pop element
int pop(struct stack *stack)
{
int item;
if(isempty(stack))
{
item=stack->array[stack->top];
stack->top--;
}
else
return(-1);
}
void main()
{
int n,val,min,max,newtop;
struct stack *stack;
clrscr();
printf("Enter number of Element of in stack");
scanf("%d",&n);
stack=creatstack(n);
while(isfull(stack))
{
system("cls"); // here you can also use clrscr();
printf("Enter Element of stack");
scanf("%d",&val);
push(stack,val);
}
newtop=stack->top; // we store top value of stack because after find min, top
will be -1 so we shall not able to acess stack again with top -1
min=pop(stack);
while(isempty(stack))
{
if(min>pop(stack)&&pop(stack)!=-1)
min=pop(stack);
}
printf("Minimum ELement is %d\n",min);
stack->top=newtop;
max=pop(stack);
while(isempty(stack))
{
if(max<pop(stack))
max=pop(stack);
}
printf("Maximum element in stack is %d",max);
getch();
}
0 Comments