Assume you are working with a doubly linked list, based on the following node: c
ID: 3771028 • Letter: A
Question
Assume you are working with a doubly linked list, based on the following node: class node float data; node next node prev; Write an insert method -which takes a "head" pointer as a parameter (along with the data to be inserted), and inserts to the head of the list. Consider the empty list case. Code public void addToHead(Node head, float data) f Node nodeTo Insert = new Node(data); Il empty list case if (headnull) lastNode nodeTolnsert; else firstNode.prev -nodeTol nsert; nodeToInsert.next firstNode; firstNode = nodeToInsert;Explanation / Answer
public void addToHead(Node head, float data) {
Node nodeToInsert = new Node(data);
// empty list case
if(head == null) {
lastNode = nodeToInsert;
}
else {
firstNode.prev = nodeToInsert;
}
nodeToInsert.next = firstNode;
nodeToInsert.prev = null;
firstNode = nodeToInsert;
}
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.