22) The class below defines a node for a linked list. Write a FUNCTION called IN
ID: 3726589 • Letter: 2
Question
22) The class below defines a node for a linked list. Write a FUNCTION called INSERT that inserts a node at the end. Assume that one node named HEAD has already been created. Your function should take any node names as arguments that the user enters in the function call and should assign data. 12 points PLEASE PUT ANSWER HERE class Node string name; int data; Node *next; public: Node() { next = NULL; } void setname(string n) (name n) void setdata(int d) (data d;) void setnext(Node*p) { next . p; } string getname) freturn name: int getdata) (return data;) Node getnext() (return 'next;) 23) FOR THE LINKED LIST BELOw, CHANGE TO MAKE secondPtr the first node and head the second node. Also make it a doubly linked list. have to change links. 10 points Note: you just double value; ListNode "next; ListNode "prev; int main() ListNode·head; head = new ListNode; head->value 12.5; head->next NULL; ListNode *secondPtr new ListNode; secondPtr->value = 13.5; secondPtr->next = NULL; head->next = secondPtr;Explanation / Answer
22) void INSERT(string name, int data) {
Node* newNode=new Node();
newNode.setname(name);
newNode.setdata(data);
Node* temp=HEAD;
//iterate till the end node
while(temp.getnext()!=NULL) {
temp=temp.getnext();
}
//attach to the last node
temp.setnext(newNode);
}
23)
//make double link
secondPtr->prev=head
head->prev=NULL
//swap pointers
ListNode* temp=head;
head=secondPtr;
secondPtr=head;
Related 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.