Stack Using Array In C Program

Ram Pothuraju
#include< stdio.h>
#include< conio.h>

#define MAX 3
void main()
{
 int S[MAX],i,val,c,top=-1,loc;
 char ch='y';
 clrscr();
 while(ch=='y')
 {
  printf("\n1.Push \n2.Pop \n3.Peep \n4.Change \n5.Display \n6.Exit");
  printf("\n Enter your choice:");
  scanf("%d",&c);


  switch(c)
  {
   case 1:
    if(top>=(MAX-1))
     printf("\n Stack Overflow");
    else
    {
     top++;
     printf("\n Enter value:");
     scanf("%d",&val);
     S[top]=val;
    }
    break;

   case 2:
    if(top==-1)
    {
     printf("\n Stack underflow");
    }

    else
    {
     printf("\n Deleted element is:%d",S[top]);
     top--;
    }
    break;
   case 3:
    printf("\n Enter index number:");
     scanf("%d",&loc);
    if((top-loc)+1<= -1)
     printf("\n Invalid index.");
    else
    {
     printf("\n At index %d element is %d",loc,S[(top-loc)+1]);

    }
    break;
   case 4:
     printf("\n Enter index number:");
     scanf("%d",&loc);
    if((top-loc)+1<= -1)
     printf("\n Stack underflow on change.");
    else
    {
     printf("\n Enter new value at location %d:",loc);
     scanf("%d",&val);
     S[(top-loc)+1]=val;

    }
    break;


   case 5:
    if(top==-1)
    {
     printf("\n Stack underflow.");
    }



    else
    {
      printf("\n Stack Content:");
      for(i=0;i<= top;i++)
      printf("\n %d",S[i]);
    }
    break;
   case 6:
    exit(0);

  }
 }
getch();
}



OUTPUT


1.Push
2.Pop
3.Peep
4.Change
5.Display
6.Exit
 Enter your choice:2

 Stack underflow
1.Push
2.Pop
3.Peep
4.Change
5.Display
6.Exit
 Enter your choice:1

 Enter value:1

1.Push
2.Pop
3.Peep
4.Change
5.Display
6.Exit
 Enter your choice:1

 Enter value:2

1.Push
2.Pop
3.Peep
4.Change
5.Display
6.Exit
 Enter your choice:1

 Enter value:3

1.Push
2.Pop
3.Peep
4.Change
5.Display
6.Exit
Enter your choice:1

 Stack Overflow
1.Push
2.Pop
3.Peep
4.Change
5.Display
6.Exit
 Enter your choice:5

 Stack Content:
 1
 2
 3
1.Push
2.Pop
3.Peep
4.Change
5.Display
6.Exit
 Enter your choice:3

 Enter index number:3

 At index 3 element is 1
1.Push
2.Pop
3.Peep
4.Change
5.Display
6.Exit
 Enter your choice:4

 Enter index number:3

 Enter new value at location 3:74

1.Push
2.Pop
3.Peep
4.Change
5.Display
6.Exit
 Enter your choice:5

 Stack Content:
 74
 2
 3
1.Push
2.Pop
3.Peep
4.Change
5.Display
6.Exit
 Enter your choice:2

 Deleted element is:3
1.Push
2.Pop
3.Peep
4.Change
5.Display
6.Exit
 Enter your choice:5

 Stack Content:
 74
 2

Post a Comment

0Comments

Post a Comment (0)