Advertisement
JHJiban

Stack Push Pop Top

Feb 25th, 2020
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct Data
  4. {
  5.     int a;
  6.     struct Data *next,*prev;
  7. }Data;
  8. Data *head=NULL;
  9.  
  10. void push(int x)
  11. {
  12.     Data *new_node=(Data*)malloc(sizeof(Data));
  13.     new_node->a=x;
  14.     new_node->next=NULL;
  15.     if(head==NULL)
  16.     {
  17.         head=new_node;
  18.         printf("Push Value %d\n",head->a);
  19.         return;
  20.     }
  21.     new_node->next=head;
  22.     head=new_node;
  23.     printf("Push Value %d\n",head->a);
  24. }
  25.  
  26. int pop()
  27. {
  28.     Data *dlt=head;
  29.     int x;
  30.     if(head==NULL)
  31.     {
  32.         printf("Empty Stack!\n");
  33.         return -1;
  34.     }
  35.     if(head->next==NULL)
  36.     {
  37.         head=NULL;
  38.         x = dlt -> a;
  39.         free(dlt);
  40.         return x;
  41.     }
  42.     head=head->next;
  43.     x = dlt -> a;
  44.     free(dlt);
  45.     return x;
  46. }
  47.  
  48. void top()
  49. {
  50.     int x;
  51.     if(head==NULL)
  52.     {
  53.         printf("Noting in Top!\n");
  54.         return;
  55.     }
  56.     x=head->a;
  57.     printf("Top Value: %d\n", x);
  58. }
  59.  
  60. int main()
  61. {
  62.     pop();
  63.     top();
  64.     push(5);
  65.     pop();
  66.     top();
  67.     push(7);
  68.     push(6);
  69.     push(5);
  70.     top();
  71.  
  72.  
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement