Answer to Question #59456 in Java | JSP | JSF for Ariel

Question #59456
Consider the following code for creating and inserting a element into a list. Assume that front is a variable that represents a reference to the first node of a list.

public void insert_number(T element) {

LinearNode<T> temp = new LinearNode<T>(element);

//insert

temp = front;

front.setNext(temp);

}

Answer the following questions:

a) Does this function correctly insert the node into the linked list? Explain. Hint: Try drawing a picture of the list.


b) Give the correct code for adding node at the head of the linked list.
1
Expert's answer
2016-08-09T09:33:03-0400
a) No, function inserts node into linked list in a wrong way. Because in linked structure first (front) node has to have link for second, second for third and so on. In this function we get the followng result. We create temp node, then we rewrite all data in this node, and make it equal to the front node, so now we have 2 front nodes. Finally we make link from front node to temp node, that means we inserted temp for second place.

b) delete this line “temp = front;”
delete this line “front.setNext(temp);”
add this line “temp.setNext(front);”

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS