Code For STACK IMPLEMENTATION.

#include<stdio.h>
#include<conio.h>
void push(int s[],int max,int *top,int x)
{
  if(*top>=max-1)
    printf("stack overflow");
  else
    {*top=*top+1;
    s[*top]=x; }
}
int pop(int s[],int *top)
{
 if(*top<=-1)
 {
  printf("stack underflow");
  return 1;
 }
 else
 {
  int item;
  item=s[*top+1];
  *top=*top-1;
  return item; }
}
int peep(int s[],int *top,int max)
{
 if((*top+1-max)<0)
   return 0;
  return s[(*top+1-max)];
}
void change(int s[],int *top,int max,int m)
{
 if((*top+1-max)<0)
  printf("stack underflow");
  else
   s[(*top+1-max)]=m;
}
int isFull(int *top,int max)
{
 if(*top>=max-1)
   return 1;
 else
  return 0;
}
int isEmpty(int *top)
{
 if(*top<=-1)
  return 1;
 else
  return 0;
}
void display(int s[],int *top)
{
 int i;
 if(*top==-1)
   printf("stack empty");
 else
 {
  printf("stack:");
  for(i=*top;i>=0;i--)
   printf("%d\t",s[i]);
 }
}
void main()
{
 int max=5,*top,i,n,N,s[5];
 *top=-1;
 clrscr();
 printf("\n1 for push\n");
 printf("2 for pop\n");
 printf("3 for peep\n");
 printf("4 for change\n");
 printf("5 for isFull\n");
 printf("6 for isEmpty\n");
 printf("7 for exit\n");
 s1:
 printf("\nenter choice:");
 scanf("%d",&N);

 switch(N)
 {
  case 1:
  printf("enter number:");
  scanf("%d",&n);
  push(s,max,top,n);
  display(s,top);
  goto s1;

  case 2:
   pop(s,top);
   display(s,top);
   goto s1;

  case 3:
   printf("enter position for element:");
   scanf("%d",&i);
   n=peep(s,top,i);
   if(n==0)
   {printf("stack underflow");}
   else
   {
    printf("element is %d",n);
    }
   display(s,top);
   goto s1;

   case 4:
    printf("enter new element:");
    scanf("%d",&n);
    printf("enter position of element:");
    scanf("%d",&i);
    change(s,top,i,n);
    display(s,top);
    goto s1;

    case 5:
     n=isFull(top,max);
     if(n==1)
      printf("stack is Full");
     else
      printf("stack is not full");
     goto s1;

    case 6:
      n=isEmpty(top);
      if(n==1)
       printf("stack is empty");
      else
       printf("stack is not empty");
      goto s1;

    case 7:
    goto s;

    default:
      printf("invalid choice");
      goto s1;

 }
  getch();
  s:
}

OUTPUT:






Comments

Popular posts from this blog

Code for BUBBLE Sort

Code for Circular Linked List c-programing

Code for SELECTION Sort in C-Programming