#include <memory>
#include <iostream>

using namespace std;

//kod poprawny
class List {
    struct Node;
    typedef std::shared_ptr<Node> PNode;
    typedef std::weak_ptr<Node> PWNode;
    struct Node {
        Node(int v) : value_(v) { cout << "C-tor " << value_ << endl; }
        ~Node() { cout << "D-tor " << value_ << endl; }
        int value_;
        PWNode me_; //wskaznik na ten sam wezel
        //PWNode next_; //element nastepny
        PNode next_; // zmiana na silny sprytny wskaznik

    };
public:
    List() {}
    ~List() {
        //wymagane 'reczne' usuniecie zaleznosci cyklicznej
        if( tail_ ) tail_->next_.reset();
    }

    void push_front(int value) {
        PNode node( new Node(value) );
        node->me_ = node;
        if( head_ ) { //not empty list
            node->next_ = head_;
            tail_->next_ = node;
        } else { //if empty list
            node->next_ = node;
            tail_ = node;
        }
        head_ = node;
    }
    void debug() {
        PNode x = head_;
        do {
            if(x) {
                cout << x->value_ << "(" << x.use_count() << "),";
                x = x->next_; //.lock();
            }
        } while (x && x != head_);
        cout << endl;
    }
private:
    PNode head_, tail_;
};

int main() {
    List l1;
    l1.debug();
    l1.push_front(1);
    l1.debug();
    l1.push_front(2);
    l1.debug();
    l1.push_front(3);
    l1.debug();
    return 0;
}
