#include <iostream>
#include <memory>

using namespace std;

struct node {
  shared_ptr<node> next;
  weak_ptr<node> wnext;
  int datum;
};

int main() {
  auto ring_buffer_head = make_shared<node>();
  auto ring_buffer_cur = ring_buffer_head;

  ring_buffer_head->datum = 0;
  for (int i = 0; i < 10; i++) {
    ring_buffer_cur->next = make_shared<node>();
    ring_buffer_cur = ring_buffer_cur->next;
    ring_buffer_cur->datum = i + 1;
  }

  // Complete the circle
  ring_buffer_cur->wnext = ring_buffer_head;

  ring_buffer_cur = ring_buffer_head;
  do {
    cout << ring_buffer_cur->datum << endl;

    if (ring_buffer_cur->next)
      ring_buffer_cur = ring_buffer_cur->next;
    else
      ring_buffer_cur = ring_buffer_cur->wnext.lock();
  } while (ring_buffer_cur != ring_buffer_head);

  return 0;
}