Advertisement
muftY

Stack

Feb 17th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. #include<stdlib.h>
  4.  
  5. typedef struct data
  6. {
  7.     int a;
  8.     struct data *next;
  9. } data;
  10. data *head=NULL;
  11.  
  12. void push(int x)
  13. {
  14.     data *first=(data*)malloc(sizeof(data));
  15.     first->a=x;
  16.     first->next=NULL;
  17.     if(head==NULL)
  18.     {
  19.         head=first;
  20.         return;
  21.     }
  22.     first->next=head;
  23.     head=first;
  24.     return;
  25.  
  26. }
  27.  
  28. int pop()
  29. {
  30.  
  31.     if(head==NULL)
  32.     {
  33.         printf("Empty\n");
  34.         return -1;
  35.     }
  36.     data *x=head->a;
  37.     data *del=head;
  38.     data *temp=head->next;
  39.     head=temp;
  40.     free(del);
  41.  
  42.     return printf("%d ",x);
  43.  
  44.  
  45.  
  46. }
  47.  
  48. int top()
  49. {
  50.     if(head==NULL)
  51.     {
  52.         printf("Empty\n");
  53.         return -1;
  54.     }
  55.     return printf("%d ",head->a);
  56. }
  57.  
  58. int main()
  59. {
  60.     /* push(6);
  61.      push(5);
  62.      push(4);
  63.      pop();
  64.      push(3);
  65.      push(2);
  66.      top();
  67.      */
  68.     while(1)
  69.     {
  70.         printf("Enter 1 for PUSH.\n");
  71.         printf("Enter 2 for POP.\n");
  72.         printf("Enter 3 for TOP.\n");
  73.         printf("Enter 0 for Quit.\n");
  74.  
  75.  
  76.  
  77.         int m,n;
  78.         scanf("%d",&m);
  79.         if(m==1)
  80.         {
  81.             printf("Plz Enter:");
  82.             scanf("%d",&n);
  83.             push(n);
  84.             printf("\n");
  85.         }
  86.         if(m==2)
  87.         {
  88.  
  89.             pop();
  90.              printf("\n");
  91.         }
  92.         if(m==3)
  93.         {
  94.  
  95.             top();
  96.              printf("\n");
  97.         }
  98.         if(m==0)
  99.         {
  100.             break;
  101.         }
  102.  
  103.  
  104.     }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement