Guest User

Untitled

a guest
Dec 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. class Node
  2. {
  3. public:
  4.  
  5. int get()
  6. {
  7. return this->Object;
  8. }
  9. void set(int value)
  10. {
  11. this->Object = value;
  12. }
  13.  
  14. void setNext(Node* value)
  15. {
  16. this->nextNode = value;
  17. }
  18.  
  19. Node *getNext()
  20. {
  21. return this->nextNode;
  22. }
  23.  
  24. private:
  25. int Object;
  26. Node* nextNode;
  27. };
  28.  
  29. class Stack
  30. {
  31.  
  32. public:
  33.  
  34. Stack()
  35. {
  36.  
  37. headNode = new Node;
  38. headNode->setNext(NULL);
  39. headNode->set(-1);
  40. }
  41.  
  42. void push(int value)
  43. {
  44. Node *newNode = new Node;
  45. newNode->set(value);
  46. newNode->setNext(headNode);
  47. headNode = newNode;
  48. }
  49.  
  50. bool isEmpty()
  51. {
  52. if((headNode->get()) != -1)
  53. {
  54. return true;
  55. }
  56. if((headNode->get()) == -1)
  57. {
  58. return false;
  59. }
  60. }
  61.  
  62. int pop()
  63. {
  64.  
  65. if(isEmpty() != false)
  66. {
  67. int x = headNode->get();
  68. Node *p = headNode;
  69. headNode = headNode->getNext();
  70. delete p;
  71. return x;
  72. }
  73. if(isEmpty() != true)
  74. {
  75. cout << "Stack is Empty";
  76. return 0;
  77. }
  78. }
  79.  
  80.  
  81. private:
  82.  
  83. Node * headNode;
  84.  
  85.  
  86. };
  87.  
  88.  
  89.  
  90. main()
  91. {
  92. Stack stk;
  93.  
  94. stk.push(23);
  95. stk.push(232);
  96.  
  97. cout << stk.pop();
  98.  
  99. system("pause");
  100. }
Add Comment
Please, Sign In to add comment