Provide code to create a linked sequence of nodes containing the values 1, 2, 3, 4, 5. The nodes should be in this order, with the “front” node containing the value 1, and the node farthest away from ““front” containing the value 5. Answer the questions below describing the outcome of the following operations using your linked structure created above
Types of Linked List
A sequence of data elements connected through links is called a linked list (LL). The elements of a linked list are nodes containing data and a reference to the next node in the list. In a linked list, the elements are stored in a non-contiguous manner and the linear order in maintained by means of a pointer associated with each node in the list which is used to point to the subsequent node in the list.
Linked List
When a set of items is organized sequentially, it is termed as list. Linked list is a list whose order is given by links from one item to the next. It contains a link to the structure containing the next item so we can say that it is a completely different way to represent a list. In linked list, each structure of the list is known as node and it consists of two fields (one for containing the item and other one is for containing the next item address).
This question relies only on creating and manipulating Node objects. The Node class code is shown (partially):
public class Node {
public int data;
public Node next;
<more code would be here>
}
Assume you have appropriate constructors to create Node objects with either default (0, null) values or user-assigned values.
You may create as many Node objects as you want to solve the following problems, but you cannot create objects of other classes. You do NOT need to create complete methods – only provide as much code as is needed to accomplish each task.
3a)
Provide code to create a linked sequence of nodes containing the values 1, 2, 3, 4, 5.
The nodes should be in this order, with the “front” node containing the value 1, and the node farthest away from ““front” containing the value 5.
Answer the questions below describing the outcome of the following operations using your linked structure created above:
Node cursor = front;
while(cursor.next != null) {
cursor = cursor.next;
}
cursor.next = front;
cursor = cursor.next;
3b)
What is the value of cursor.data?
3.c)
Describe the form of the linked structure you have created (how many nodes, what order they have, where front and cursor are located, etc.).
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images