2/23/2009

C Program To Perform Push(), Pop() and Display() Operation On The Stack


/*C Program To Perform Push(), Pop() and Display() Operation On The Stack */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define stack_size 4
int item,s[10],top=-1;
void push();
int pop();
void display();
void main()
{
int item_deleted,ch;
 clrscr();
 for(; ;)
  {
   printf("1:push 2:pop 3:display 4:exit\n");
   printf("Enter your choice\n");
    scanf("%d",&ch);
   switch(ch)
    {
    case 1:
          printf("Enter the item to be inserted\n");
     scanf("%d",&item);
     push();
     break;
    case 2:
     item_deleted=pop();
     if(item_deleted==0)
      printf("Underflow\n");
     else
      printf("Item deleted=%d\n",item_deleted);
      break;
    case 3:
     display();
     break;
    default:
     exit(0);
    }
   getch();
  }
}

void push()
{
 if(top==stack_size-1)
  {
   printf("Stack overflow\n");
   return;
  }
 top=top+1;
 s[top]=item;
}

int pop()
{
 int item_deleted;
  if(top==-1)
   return 0;
  item_deleted=s[top--];
  return item_deleted;
}

void display()
{
 int i;
  if(top==-1)
   {
   printf("Stack is empty\n");
   return;
   }
  printf("Contents of the stack are\n");
  for(i=0;i<=top;i++)
   {
   printf("%d\n",s[i]);
   }
}
Out Put:
C Program To Perform Push(), Pop() and Display() Operation On The Stack

No comments:

Post a Comment