#include using namespace std; // Snake class Snake { private: class Segment; //Forward declare Segment public: Snake(int x, int y): head{new Segment{x, y}} {} void print() const { Segment* current = head; while( current != nullptr ) { current -> print(); current = current -> get_next(); } cout << "null"; } void add(int x, int y) { //call the recursive function in first Segment head -> add(new Segment{x, y}); } private: Segment* head; class Segment //Segment is an internal class { //Not visible outside of Snake public: //User cannot create segments in main() Segment(int x, int y): x{x}, y{y}, next{nullptr} {} void add(Segment * s) { //If there is no next segment //add the new segment here if (next == nullptr) { next = s; } else // Otherwise call add in next segment { next -> add(s); } } void print() const { cout << "(" << x << ", " << y << ") --> "; } Segment* get_next() const { return next; } private: int x; int y; Segment* next; //Pointer to next segment }; }; int main() { Snake snake{7, 7}; snake.print(); cout << "\n========" << endl; snake.add(7, 6); snake.print(); cout << endl; }