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 << " ";
Explanation / Answer
class Node { public: Node(); // constructor of class node without arguments Node(int, Node*); //constructor with arguments of int type and a pointer node type Node* next() const; void setNext(Node*); //member fun, returning a pointer of node type void setKey(int); int getKey() const; int done; private: Node* ptr; int key; }; Node::Node() // constructor intialisation { ptr = NULL; key = -1; done = 1; } Node::Node(int tkey, Node* tnode) // intialisation to private members { ptr = tnode; key = tkey; done = 1; } Node* Node::next() const //returning a pointer of type node { return ptr; } int Node::getKey() const // returning the key to be inserted { return key; } void Node::setNext(Node* tnode) // setting the link to next node in ptr { ptr = tnode; } void Node::setKey(int tkey) // setting the value to be inserted in key { key = tkey; } void insert(Node* head, int key) // function taking the starting pointer of list and key to be inserted { Node* temp = new Node(key, NULL); // creating a new node by calling function Node(int, Node*) Node* index; if(key getKey()) // comparing the key to be inserted and present starting key of the list { index = new Node(key, head); // if true, make the key to be inserted as the starting key head = index; return; } index = head; while(index->next()!=NULL && index->next()->getKey() next(); // when found the place to insert the key, store the pointer in index. } temp->setNext(index->next()); // point temp->link to index. index->setNext(temp); return; } void printList(Node * head) // print the list, starting form head of the list { Node* index = head; // storing value head in pointer index cout next is not equal to NULL { index = index->next(); //traversing node by node coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.