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

Consider a linked list whose nodes are objects of the class Node: class Node {    ...
Consider a linked list whose nodes are objects of the class Node: class Node {     public int data;     public Node next; } prev references a node n1 in the list and curr references the node n2 that is right after n1 in the list. Which of the following statements is used to insert a new node, referenced by newNodebetween prev and curr? Group of answer choices newNode.next = curr; prev.next = newNode; newNode.next = head; head = newNode;...
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...
1a) Write the start of the class declaration for a node in a linked list (give...
1a) Write the start of the class declaration for a node in a linked list (give the name of the class and the instance variables). The name of the node should be SpecialNode. The data in each SpecialNode will include both a String and a Song object. b)Using the SpecialNode class you created in question above, write a constructor forSpecialNode that has three parameters and initializes all the SpecialNode instance variables. c) Write a line of code to instantiate a...
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...
In Python, I've created a Node class for implementing a singly linked list. My Code: class...
In Python, I've created a Node class for implementing a singly linked list. My Code: class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self,newdata): self.data = newdata def setNext(self,newnext): self.next = newnext class SinglyLinkedList: def __init__(self): self.head = None def add(self,key): addkey = Node(key) addkey.setNext(self.head) self.head = addkey Now the question is: Create an append method that is O(1) by modifying the constructor of the SinglyLinkedList class by adding...
In short, you’re going to implement a linked-list class for storing integers, using a provided main...
In short, you’re going to implement a linked-list class for storing integers, using a provided main program to help you interact and test your work. You’ll want to build the linked-list class function by function, working in “Develop” mode to test out each function you write. main.cpp is a read only file linkedlist.h is the file to work on. main.cpp #include #include #include "linkedlist.h" using namespace std; int main() { linkedlist LL; string cmd; int value, key; // // user...
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...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT