#include <array>
#include <cassert>
#include <iostream>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <vector>
class LinkedStack {
  struct Node {
    int x;
    std::unique_ptr<Node> next;
    Node(int v, std::unique_ptr<Node> p) : x(v), next(std::move(p)) {}
  };
  std::unique_ptr<Node> head;

public:
  void push(int x) { head = std::make_unique<Node>(x, std::move(head)); }
  std::optional<int> pop() {
    if (!head)
      return {};
    int x = head->x;
    head = std::move(head->next);
    return x;
  }
  ~LinkedStack() {
    while (head)
      head = std::move(head->next);
  }
};
class RingQueue {
  std::array<int, 5> a{};
  int front = 0, rear = 0;

public:
  int size() const { return (rear - front + 5) % 5; }
  bool push(int x) {
    if ((rear + 1) % 5 == front)
      return false;
    a[rear] = x;
    rear = (rear + 1) % 5;
    return true;
  }
  std::optional<int> pop() {
    if (front == rear)
      return {};
    int x = a[front];
    front = (front + 1) % 5;
    return x;
  }
};
bool brackets(const std::string &s) {
  std::vector<char> a;
  for (char c : s) {
    if (c == '(' || c == '[' || c == '{')
      a.push_back(c);
    else if (c == ')' || c == ']' || c == '}') {
      if (a.empty())
        return false;
      char t = a.back();
      a.pop_back();
      if (!((t == '(' && c == ')') || (t == '[' && c == ']') ||
            (t == '{' && c == '}')))
        return false;
    }
  }
  return a.empty();
}
// 教学范围：空格分隔的整数 token、二元 + - * /；调用者保证运算不溢出。
long long postfix(const std::vector<std::string> &tokens) {
  std::vector<long long> s;
  for (const auto &t : tokens) {
    if (t == "+" || t == "-" || t == "*" || t == "/") {
      if (s.size() < 2)
        throw std::invalid_argument("missing operand");
      auto b = s.back();
      s.pop_back();
      auto a = s.back();
      s.pop_back();
      if (t == "/" && b == 0)
        throw std::domain_error("division by zero");
      s.push_back(t == "+"   ? a + b
                  : t == "-" ? a - b
                  : t == "*" ? a * b
                             : a / b);
    } else {
      std::size_t used = 0;
      auto n = std::stoll(t, &used);
      if (used != t.size())
        throw std::invalid_argument("invalid token");
      s.push_back(n);
    }
  }
  if (s.size() != 1)
    throw std::invalid_argument("invalid expression");
  return s.back();
}
int main() {
  LinkedStack s;
  assert(!s.pop());
  s.push(1);
  s.push(2);
  assert(s.pop() == 2 && s.pop() == 1 && !s.pop());
  RingQueue q;
  assert(!q.pop());
  for (int x : {1, 2, 3, 4})
    assert(q.push(x));
  assert(!q.push(5));
  assert(q.pop() == 1 && q.pop() == 2);
  assert(q.push(5) && q.push(6));
  for (int x : {3, 4, 5, 6})
    assert(q.pop() == x);
  assert(!q.pop());
  assert(brackets("{[()]}"));
  assert(!brackets("([)]") && !brackets(")"));
  assert(postfix({"12", "3", "4", "*", "+"}) == 24);
  assert(postfix({"8", "3", "-"}) == 5);
  bool caught = false;
  try {
    postfix({"1", "0", "/"});
  } catch (const std::domain_error &) {
    caught = true;
  }
  assert(caught);
  std::cout << "chapter04: passed\n";
}
