#include #include using namespace std; class Snake { private: struct Segment; public: Snake(int x, int y): head{new Segment{x, y, nullptr}} {} Snake(Snake const& other): head{nullptr} { Segment* current_other{other.head}; head = new Segment{current_other->x, current_other->y, nullptr}; Segment* current{head}; while(current_other->next != nullptr) { current_other = current_other->next; current->next = new Segment{current_other->x, current_other->y, nullptr}; current = current->next; } } ~Snake() { Segment* current{head}; Segment* current_next{head->next}; while(true) { delete current; current = current_next; if(current == nullptr) { break; } current_next = current_next->next; } } void add(int x, int y) { recursive_add(x, y, head); } void print() const { Segment* current{head}; while(true) { cout << "x: " << current -> x << ", y: " << current -> y << endl; current = current -> next; if( current == nullptr) { break; } } } private: Segment* head; struct Segment { int x; int y; Segment* next; }; void recursive_add(int x, int y, Segment* current) { if(current->next == nullptr) { Segment* tmp{new Segment{x, y, nullptr}}; current->next = tmp; } else { recursive_add(x, y, current->next); } } }; int main() { Snake s{1, 1}; s.add(1, 2); s.add(2, 2); s.print(); cout << endl; Snake s2{s}; s2.print(); }