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);”
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
APPROVED BY CLIENTS
"assignmentexpert.com" is professional group of people in Math subjects! They did assignments in very high level of mathematical modelling in the best quality. Thanks a lot
Comments
Leave a comment