a program for stack sort (push and pop)

#include <stdio.h>
#define MAX 5
int top, status;
void push (int stack[], int item)
{ if (top == (MAX-1))
status = 0;
else
{ status = 1;
++top;
stack [top] = item;
}
}
int pop (int stack[])
{
int ret;
if (top == -1)
{
status = 0;
}
else
{
status = 1;
ret = stack [top];
–top;
}
return ret;
}
void display (int stack[])
{ int i;
printf (“\nThe Stack is: \n”);
if (top == -1)
printf (“empty”);
else
{
for (i=top; i>=0; –i)
printf (” \n%d “,stack[i]);
}
printf (“\n”);
}
int main()
{
int stack [MAX], item;
int ch;
top = -1;
do
{ do
{
printf (“\nNMAIN MENU:\n”);
printf (“\n 1.PUSH :”);
printf (“\n 2.POP :”);
printf (“\n 3.Exit :”);
printf (“\nEnter Your Choice:\n “);
scanf (“%d”, &ch);
if (ch<1 || ch>3)
printf (“\nInvalid Choice, Please try again:\n”);
} while (ch<1 || ch>3);
switch (ch)
{
case 1:
printf (“\nEnter the Element to be pushed : \n”);
scanf (“%d”, &item);
push (stack, item);
if (status)
{ printf (“\nAfter Pushing :\n”);
display (stack);
if (top == (MAX-1))
printf (“\nThe Stack is Full:\n”);
}
else
printf (“\nStack overflow on Push:\n”);
break;
case 2:
item = pop (stack);
if (status)
{ printf (“\nThe Popped item is %d \n After Popping: “,item);
display (stack);
}
else
printf (“\nStack underflow on Pop:\n”);
break;

}
}while (ch != 3);
}

Leave a comment