Skip to main content

Dynamic Implementation Of Stack

Dynamic Implementation Of Stack


CODE
👇
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct node
  4. {
  5.       int info;
  6.       struct node *next;
  7. }NODE;
  8. NODE *top;

  9. void initstack()
  10. {
  11.      top=NULL;
  12. }

  13. int isempty()
  14. {
  15.       return(top==NULL);

  16. void push()
  17. {
  18.     int num;
  19.     NODE*  nn=(NODE*)malloc(sizeof(NODE));
  20.     nn->info=num;
  21.     nn->next=NULL;
  22.     nn->next=top;
  23.     top=nn;
  24. }

  25. int pop()
  26. {
  27.     int num;
  28.     NODE * temp = top;
  29.     num = top -> info;
  30.     top = top -> next;
  31.     free (temp);
  32.     return num;
  33. }

  34. void main()
  35. {
  36.     int choice, n;
  37.     initstack();
  38.     do
  39.     {
  40.         printf("\n1.PUSH \n2.POP \n3.EXIT\n");
  41.         printf("Enter Your Choice:");
  42.         scanf("%d",&choice);
  43.         switch(choice)
  44.         {
  45.             case 1:/*PUSH*/
  46.                 printf("\n ENTER THE TO BE PUSHED:");
  47.                 scanf("%d",&n);
  48.                 push(n);
  49.                 break;

  50.             case 2:/*POP*/
  51.                 if(isempty())
  52.                 printf("\n >>STACK IS EMPTY<<");
  53.                 else
  54.                 printf("\n POPPED ELEMENT:%d",pop());
  55.                 break;
  56.         
  57.             default:
  58.                 printf("\n>>ENTER THE RIGHT CHOICE<<\n");
  59.         }
  60.     }while(choice!=3);
  61. }


👉Execute👈

/*
OUTPUT

1.PUSH 
2.POP 
3.EXIT
Enter Your Choice:1

 ENTER THE TO BE PUSHED:5

1.PUSH 
2.POP 
3.EXIT
Enter Your Choice:2

 POPPED ELEMENT:
1.PUSH 
2.POP 
3.EXIT
Enter Your Choice:4

>>ENTER THE RIGHT CHOICE<<

1.PUSH 
2.POP 
3.EXIT
Enter Your Choice:3


*/







                                                                    //ThE ProFessoR

Comments