Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct node
  5. {
  6.     node * next;
  7.     int val;
  8. };
  9.  
  10. bool isEmpty(node *stack)
  11. {
  12.     if (stack)
  13.     {
  14.         return false;
  15.     }
  16.     else return true;
  17. }
  18. void showTop(node *stack)
  19. {  
  20.     if (isEmpty(stack))
  21.     {
  22.         cout << "stack is empty!" << endl;
  23.     }
  24.     else cout << stack->val<<endl;
  25. }
  26.  
  27. void push(node *&stack, int v)
  28. {
  29.     node * e = new node;
  30.     e->val = v;
  31.     e->next = stack;
  32.     stack = e;
  33. }
  34. void pop(node *&stack)
  35. {
  36.     if (!isEmpty(stack))
  37.     {
  38.         node * e = stack;
  39.         stack = stack->next;
  40.         delete e;
  41.     }
  42. }
  43.  
  44.  
  45. int main()
  46. {
  47.     node * head = nullptr;
  48.     node * tail = nullptr;
  49.     node * stack = nullptr;
  50.     pop(stack);
  51.     showTop(stack);
  52.     pop(stack);
  53.     showTop(stack);
  54.     push(stack, 5);
  55.     showTop(stack);
  56.     push(stack, 1);
  57.     showTop(stack);
  58.     push(stack, 3);
  59.     showTop(stack);
  60.     pop(stack);
  61.     showTop(stack);
  62.  
  63.  
  64.  
  65.     system("pause");
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement