Nayeemzaman

stack1

Apr 3rd, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define TRUE 1
  4. #define FALSE 0
  5.  
  6. struct node
  7. {
  8.     int data;
  9.     struct node *next;
  10. };
  11. typedef struct node node;
  12.  
  13. node *top;
  14.  
  15. void initialize()
  16. {
  17.     top = NULL;
  18. }
  19.  
  20. void push(int value)
  21. {
  22.     node *tmp;
  23.     tmp = malloc(sizeof(node));
  24.     tmp -> data = value;
  25.     tmp -> next = top;
  26.     top = tmp;
  27. }
  28.  
  29. int pop()
  30. {
  31.     node *tmp;
  32.     int n;
  33.     tmp = top;
  34.     n = tmp->data;
  35.     top = top->next;
  36.     free(tmp);
  37.     return n;
  38. }
  39.  
  40. int Top()
  41. {
  42.     return top->data;
  43. }
  44.  
  45. int isempty()
  46. {
  47.     return top==NULL;
  48. }
  49.  
  50. void display(node *head)
  51. {
  52.     if(head == NULL)
  53.     {
  54.         printf("NULL\n");
  55.     }
  56.     else
  57.     {
  58.         printf("%d\n", head -> data);
  59.         display(head->next);
  60.     }
  61. }
  62.  
  63. int main()
  64. {
  65.     initialize();
  66.     push(10);
  67.     push(20);
  68.     push(30);
  69.     printf("The top is %d\n",Top());
  70.     pop();
  71.     pop();
  72.     printf("The top after pop is %d\n",Top());
  73.     display(top);
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment