Question

In: Computer Science

//LinkNode is a class for storing a single node of a linked list storing integer values....

//LinkNode is a class for storing a single node of a linked list storing integer values. It has two public data fields for the data and the link to

//the next node in the list and has three constructors:

public class LinkNode {

public int data;      

public LinkNode next;

// post: constructs a node with data 0 and null link

public ListNode() {

     this(0, null);

}

// post: constructs a node with given data and null link

public LinkNode (int data) {

     this(data, null);

}

// post: constructs a node with given data and given link

public LinkNode (int data, LinkNode next) {

     this.data = data;

     this.next = next;

}

}

Assume:

LinkNode list = new LinkNode( );

  1. What would the given linked node diagram look like after the given code executes:

a.

           +----+----+    +----+----+

list ----> | 1 | +----> | 2 | / |

           +----+----+    +----+----+

List.next = new LinkNode(9);

Draw your diagram here…

b.

           +----+----+    +----+----+

list ----> | 1 | +----> | 2 | / |

           +----+----+    +----+----+

List.next = new LinkNode(9, list.next);

Draw your diagram here…

c.

           +----+----+    +----+----+    +----+----+

list ----> | 7 | +----> | 8 | +----> | 3 | / |

           +----+----+    +----+----+    +----+----+

List = new LinkNode(5, list.next.next);

Draw your diagram here…

Solutions

Expert Solution

a) Ans:

Here we have list pointing to 1 initally. List.next is changed to a new node with data 9, hence the new linked node diagram would be:



           +----+----+    +----+----+

list ----> | 1 | +----> | 9 | / |

           +----+----+    +----+----+

===================================

b) Ans:

new LinkNode(9, list.next); ==> this will create a node with value 9 and next pointing to node with data 2 of list.

Now List.next is set to above value. Hence new list would be


           +----+----+    +----+----+    +----+----+

list ----> | 1 | +----> | 9 | +----> | 2 | / |

           +----+----+    +----+----+    +----+----+

=================================

c) Ans

List = new LinkNode(5, list.next.next);

A new link node with value 5 is created. The next value is set to node pointing at 3 of the orignal list. Hence new list would be



           +----+----+    +----+----+

list ----> | 5 | +---->   | 3 | / |

           +----+----+    +----+----+


==========

For any query comment.


Related Solutions

You're given the pointer to the head node of a ordered linked list, an integer to...
You're given the pointer to the head node of a ordered linked list, an integer to add to the list. Write a function that inserts a number in the the list preserving its order. If the head pointer contains a null pointer that indicates an empty list. Function insertNode has the following parameters: head: a SinglyLinkedListNode pointer to the head of the list data: an integer value to insert as data in your new node Function prototype: SinglyLinkedListNode* insertNode(SinglyLinkedListNode* head,...
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """ def __init__(self, value, next=None, prev=None): """ Constructor @attribute value: the value to give this node @attribute next: the next node for this node @attribute prev: the previous node for this node """ self.__next = next self.__prev = prev self.__value = value def __repr__(self): return str(self.__value) def __str__(self): return str(self.__value) def get_value(self): """ Getter for value :return: the value of the node """ return self.__value...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
Purpose Purpose is to implement some single linked list methods. Add methods to the List class...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class In the ‘Implementation of linked lists’ lecture, review the ‘Dynamic implementation of single linked list’ section. You will be adding new methods to the List class. Eight new methods are required: new constructor – creates a new single linked list from an array of integers e.g. int a[] = {1, 2, 3, 4}; List list = new List(a); toString() – returns a string representing...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
9.7 LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class,...
9.7 LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 Sortedlist.java import java.util.Scanner; public class SortedList { public static void main (String[] args)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT