193030

Stack push display pop

Sep 24th, 2020
1,133
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. using namespace std;
  5.  
  6. struct Node
  7. {
  8.     int data;
  9.     struct Node* next;
  10. };
  11.  
  12. Node* head = NULL;
  13.  
  14. void push(int x)
  15. {
  16.     Node *t = (struct Node*)malloc(sizeof(struct Node));
  17.     t->data = x;
  18.     t->next = head;
  19.     head = t;
  20. };
  21.  
  22. void Display(struct Node *p)
  23. {
  24.     while(p!=NULL)
  25.     {
  26.         cout << p->data << endl;
  27.         p = p->next;
  28.     }
  29. }
  30. void pop(int n)
  31. {
  32.     for(int i =0; i<n; i++)
  33.     {
  34.         Node *t = (struct Node*)malloc(sizeof(struct Node));
  35.         t = head;
  36.         head = head -> next;
  37.         delete t;
  38.     }
  39. }
  40. int main()
  41. {
  42.     push(10); push(0);
  43.     Display(head);
  44.     pop(1);
  45.     Display(head);
  46. }
  47.  
RAW Paste Data