Program to Perform different Stack Operations


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 5

int stack[max];
int top=-1;

void push();
void pop();
void display();

main()
{
 int choice1,choice2;
 clrscr();

 do
 {
  printf("\n Enter the operation \n");
  printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
  scanf("%d",&choice1);

  switch(choice1)
  {
   case 1:
   push();
   break;
   case 2:
   pop();
   break;
   case 3:
   display();
   break;
   case 4:
   exit(0);
   break;
   default:
   printf("\n Please enter a valid choice \n");
  }
  printf("\n Press 1 to Continue");
  scanf("%d",&choice2);
 }
  while(choice2==1);

  if(choice2!=1)
  exit(0);

  getch();
 }

void push()
{
 int item;
 if(top==(max-1))
 printf("\n Stack is full \n");
 else
 {
 printf("\n enter the item \n");
 scanf("%d",&item);

 top=top+1;
 stack[top]=item;
 printf("\n the pushed item is %d",stack[top]);
 }
}

void pop()
{
 int item;
 if(top==-1)
 printf("\n Stack is empty \n");
 else
 {
 item=stack[top];
 top=top-1;
 printf("\n the poped item is %d \n",item);
 }
}

void display()
{
 int i;
 printf("\n Displaying the elemets \n");

 for(i=0;i<=top;i++)
 printf("\n %d",stack[i]);
}






0 comments:

Post a Comment