Can you explain this code line by line? I am not 100% sure how this program work
ID: 3553032 • Letter: C
Question
Can you explain this code line by line? I am not 100% sure how this program works.
This program is a linked list with a node class. The node class keeps a key and pointer as private variables. Additionally, It has a function in the ?le containing the main function that inserts a new node in a sorted linked list. In the main function, create a linklist using this function and class to add the values 0, 1, 4, 7, 10. After, print them from the linked list. class Node { public: Node(); Node(int, Node*); Node* next() const; void setNext(Node*); void setKey(int); int getKey() const; int done; private: Node* ptr; int key; }; Node::Node() { ptr = NULL; key = -1; done = 1; } Node::Node(int tkey, Node* tnode) { ptr = tnode; key = tkey; done = 1; } Node* Node::next() const { return ptr; } int Node::getKey() const { return key; } void Node::setNext(Node* tnode) { ptr = tnode; } void Node::setKey(int tkey) { key = tkey; } void insert(Node* head, int key) { Node* temp = new Node(key, NULL); Node* index; if(key < head->getKey()) { index = new Node(key, head); head = index; return; } index = head; while(index->next()!=NULL && index->next()->getKey() < key) { index = index->next(); } temp->setNext(index->next()); index->setNext(temp); return; } void printList(Node * head) { Node* index = head; cout << index->getKey() << ", "; while(index->next()!= NULL) { index = index->next(); cout << index->getKey() << ", "; } cout << " ";