top of page

Minimum & Maximum Element in a Stack

Writer's picture: Code CrazeCode Craze


#include <stdio.h>

#include<stdlib.h>

#include<math.h>


#define size 100

int stack[size];

int top=-1;


void push(void);

void pop(void);

void peek_top(void);

void isEmpty(void);

void isFull(void);

void display(void);

void max(void);

void min(void);


void main() {

int c,e;

do{

switch(c)

{

case 1:

push();

break;

case 2:

pop();

break;

case 3:

peek_top();

break;

case 4:

isEmpty();

break;

case 5:

isFull();

break;

case 6:

display();

break;

case 7:

max();

break;

case 8:

min();

break;

case 9:

printf("\n\n----Exit from program successfully----");

printf("\n--------------Thank you--------------");

exit(0);

}

printf("\n1) Push\t2) Pop\t3) Peek\t4) isEmpty\t5) isFull\t6) Display\t7) Maximum\t8) Minimum\t9) Exit");

printf("\n\nEnter your choice : ");

scanf("%d",&c);

}while(c);

}


void push()

{

int v;

printf("\nEnter value : ");

scanf("%d",&v);

if(top==size-1)

{

printf("\nStack is full!!");

}

else

{

top++;

stack[top]=v;

}

}


void display()

{

int i;

if(top==-1)

{

printf("\nStack is empty");

}

else

{

for(i=0;i<=top;i++)

{

printf("--%d",stack[i]);

}

}

}


void pop()

{

if(top==-1)

{

printf("\nStack is empty!!");

}

else

{

top--;

}

}


void peek_top()

{

printf("\nTop : %d",top);

printf("\nTop Value: %d",stack[top]);

}


void isEmpty()

{

if(top==-1)

{

printf("\nStack is empty!!");

}

else

{

printf("\nStack is not empty!!");

}

}


void isFull()

{

if(top==size-1)

{

printf("\nStack is full!!");

}

else

{

printf("\nStack is not full!!");

}

}


void max()

{

int mx=stack[0];

int i;

for(i=0;i<=top;i++)

{

if(mx<stack[i])

{

mx=stack[i];

}

}

printf("\nMaximum value : %d",mx);

}


void min()

{

int mn=stack[0];

int i;

for(i=0;i<=top;i++)

{

if(mn>stack[i])

{

mn=stack[i];

}

}

printf("\nMinimum value : %d",mn);

}

2 views0 comments

Recent Posts

See All

Stack Using Queue

A stack can be implemented using two queues in C. The basic idea is to use one queue for enqueue operations and another for dequeue...

Comments


bottom of page