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. } 

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

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

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

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


👉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

Another posts