Yakov

FIVT2020.Seminar2

Sep 21st, 2020
728
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cassert>
  3.  
  4. class Stack {
  5. public:
  6.     Stack(const Stack& other) = delete;
  7.     Stack(Stack&& other) = delete;
  8.     ~Stack();
  9.  
  10.     Stack& operator=(const Stack& other) = delete;
  11.     Stack& operator=(Stack&& other) = delete;
  12.  
  13.     void Push(int value);
  14.     int Pop();
  15.     const int& Top() const;
  16.     bool IsEmpty() const { return !head_; }
  17.  
  18. private:
  19.     struct Node {
  20.         int Value;
  21.         Node* Next;
  22.  
  23.         Node(int value, Node* next = nullptr) : Value(value), Next(next) {}
  24.     };
  25.  
  26.     Node* head_ = nullptr;
  27. };
  28.  
  29. Stack::~Stack() {
  30.     while (head_) {
  31.         auto next = head_->Next;
  32.         delete head_;
  33.         head_ = next;
  34.     }
  35. }
  36.  
  37. void Stack::Push(int value)
  38. {
  39.     head_ = new Node(value, head_);
  40. }
  41.  
  42. int Stack::Pop()
  43. {
  44.     assert(!IsEmpty());
  45.  
  46.     auto next = head_->Next;
  47.     int value = head_->Value;
  48.     delete head_;
  49.     head_ = next;
  50.     return value;
  51. }
  52.  
  53. const int& Stack::Top() const
  54. {
  55.     assert(!IsEmpty());
  56.     return head_->Value; // (*head_).Value
  57. }
  58.  
  59. void g(Stack& s)
  60. {
  61.     s.Push(1);
  62.     s.Push(2);
  63. }
  64.  
  65. int f() {
  66.     Stack s;
  67.     s.Push(1);
  68.     s.Push(2);
  69.     s.Push(3);
  70.     s.Push(4);
  71.     g(s);
  72.     std::cout << s.Top();
  73.     std::cout << " ";
  74.     std::cout << s.Pop();
  75.     std::cout << s.Pop();
  76.     return 0;
  77. }
  78.  
  79. int main()
  80. {
  81.     f();
  82.  
  83.     return 0;
  84. }
  85. //int main()
  86. //{
  87. //  const int i = 3;
  88. //  int sum = 25;
  89. //  int a[5] = { 0, 1, 2, 3, 4 };
  90. //  unsigned int b = i;
  91. //  double d = sum;
  92. //  float f = static_cast<float>(sum);
  93. //  std::cout << sum << " " << i + i << std::endl;
  94. //  double* pd = &d;
  95. //  const int* pi = &(a[0]);
  96. //  std::cout << a[1] << " " << *(a + 1) << " "
  97. //      << *(1 + a) << " " << 1[a] << std::endl;
  98. //  ++pi;
  99. //  std::cout << *pi << " " << pi[-1] << " " << pi - a;
  100. //  return 0;
  101. //}
  102. //
  103.  
RAW Paste Data