Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cassert>
- class Stack {
- public:
- Stack(const Stack& other) = delete;
- Stack(Stack&& other) = delete;
- ~Stack();
- Stack& operator=(const Stack& other) = delete;
- Stack& operator=(Stack&& other) = delete;
- void Push(int value);
- int Pop();
- const int& Top() const;
- bool IsEmpty() const { return !head_; }
- private:
- struct Node {
- int Value;
- Node* Next;
- Node(int value, Node* next = nullptr) : Value(value), Next(next) {}
- };
- Node* head_ = nullptr;
- };
- Stack::~Stack() {
- while (head_) {
- auto next = head_->Next;
- delete head_;
- head_ = next;
- }
- }
- void Stack::Push(int value)
- {
- head_ = new Node(value, head_);
- }
- int Stack::Pop()
- {
- assert(!IsEmpty());
- auto next = head_->Next;
- int value = head_->Value;
- delete head_;
- head_ = next;
- return value;
- }
- const int& Stack::Top() const
- {
- assert(!IsEmpty());
- return head_->Value; // (*head_).Value
- }
- void g(Stack& s)
- {
- s.Push(1);
- s.Push(2);
- }
- int f() {
- Stack s;
- s.Push(1);
- s.Push(2);
- s.Push(3);
- s.Push(4);
- g(s);
- std::cout << s.Top();
- std::cout << " ";
- std::cout << s.Pop();
- std::cout << s.Pop();
- return 0;
- }
- int main()
- {
- f();
- return 0;
- }
- //int main()
- //{
- // const int i = 3;
- // int sum = 25;
- // int a[5] = { 0, 1, 2, 3, 4 };
- // unsigned int b = i;
- // double d = sum;
- // float f = static_cast<float>(sum);
- // std::cout << sum << " " << i + i << std::endl;
- // double* pd = &d;
- // const int* pi = &(a[0]);
- // std::cout << a[1] << " " << *(a + 1) << " "
- // << *(1 + a) << " " << 1[a] << std::endl;
- // ++pi;
- // std::cout << *pi << " " << pi[-1] << " " << pi - a;
- // return 0;
- //}
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement