Guest User

Untitled

a guest
Aug 17th, 2020
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct Node {
  4. int value;
  5. Node *next;
  6. Node():value(0),next(nullptr) {}
  7. };
  8. class Stack {
  9. Node *head;
  10. public:
  11. Stack() : head(nullptr) {}
  12.  
  13. void push(int x) {
  14. Node *tmp = new Node;
  15. tmp->value = x;
  16. tmp->next = head;
  17. head = tmp;
  18. }
  19.  
  20. int top() {
  21. checker();
  22. return head->value;
  23. }
  24.  
  25. void pop() {
  26. checker();
  27. Node *tmp = head;
  28. head = head->next;
  29. delete tmp;
  30. }
  31.  
  32. Stack (const Stack& other):
  33.  
  34. {}
  35.  
  36. ~Stack() {
  37. while (head != nullptr) {
  38. pop();
  39. }
  40. }
  41.  
  42. private:
  43. void checker()
  44. {
  45. if (head==nullptr)
  46. {
  47. throw std::runtime_error(" Called on empty list");
  48. }
  49. }
  50. };
  51.  
  52. int main()
  53. {
  54. Stack s;
  55. s.top();
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment