Stack in c programming

0
Stack in c programming
The C Program is written for implementation of STACK using Array, the basic operations of stack are PUSH(), POP() and DISPLAY(). PUSH function in the code is used to insert an element to the top of stack, POP function used to remove the element from the top of stack. Finally display function used to print the values at any time. All stack functions are implemented in C Code.
  1. #include<stdio.h>
  2. int stack[100],choice,n,top,x,i;
  3. int main()
  4. {

  5.     //top=-1;

  6.      printf("\n\t###############################");
  7.     printf("\n\t STACK OPERATIONS USING ARRAY");

  8.     printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
  9.     do
  10.     {
  11.         printf("\n Enter the Choice:");
  12.         scanf("%d",&choice);
  13.         switch(choice)
  14.         {
  15.             case 1:
  16.             {
  17.                 push();
  18.                 break;
  19.             }
  20.             case 2:
  21.             {
  22.                 pop();
  23.                 break;
  24.             }
  25.             case 3:
  26.             {
  27.                 display();
  28.                 break;
  29.             }
  30.             case 4:
  31.             {
  32.                 printf("\n\t EXIT POINT ");
  33.                 break;
  34.             }
  35.             default:
  36.             {
  37.                 printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
  38.             }

  39.         }
  40.     }
  41.     while(choice!=4);
  42.     return 0;
  43. }
  44. void push()
  45. {
  46.     if(top>=n-1)
  47.     {
  48.         printf("\n\tSTACK is over flow");

  49.     }
  50.     else
  51.     {
  52.         printf(" Enter a value to be pushed:");
  53.         scanf("%d",&x);
  54.         top++;
  55.         stack[top]=x;
  56.     }
  57. }
  58. void pop()
  59. {
  60.     if(top<=-1)
  61.     {
  62.         printf("\n\t Stack is under flow");
  63.     }
  64.     else
  65.     {
  66.         printf("\n\t The popped elements is %d",stack[top]);
  67.         top--;
  68.     }
  69. }
  70. void display()
  71. {
  72.     if(top>=0)
  73.     {
  74.         printf("\n The elements in STACK \n");
  75.         for(i=top; i>=0; i--)
  76.             printf("\n%d",stack[i]);
  77.         printf("\n Press Next Choice");
  78.     }
  79.     else
  80.     {
  81.         printf("\n The STACK is empty");
  82.     }

  83. }


Post a Comment

0Comments
Post a Comment (0)