Question

In: Computer Science

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;

prev.next = newNode;
newNode.next = prev;

prev.next = curr;
newNode.next = curr;

none of the above

Which of the following statements deletes the first node of a linked list implementation of ListInterface that has 10 nodes? Assume curr refers to the second node in the list and the head of the list is the reference variable fistNode

Group of answer choices

A. firstNode.next = curr.next;

B. firstNode = curr.next;

C. firstNode = firstNode.next;

D. curr = firstNode.next;

E. B and C

In LList2 implementation of the ListInterface,

Group of answer choices

A. The worst-case runtime of contains() is independent of the number of items in LinkedList

B. The runtime of getLength() is independent of the number of items in LinkedList

C. The runtime of remove(1) (removing the first element) is independent of the number of items in LinkedList

D. A and B

E. B and C

In a linked implementation of ListInterface, if the reference to the first node is null, this means

Group of answer choices

the list is full

the garbage collector should be invoked

we can't add items to the list

the list is in an unstable state

the list is empty

Solutions

Expert Solution

Answer 1 :

If prev references a node n1 in the list and curr references the node n2 that is right after n1 in the list.

Then , statements required to insert a new node, referenced by newNode between prev and curr are :

newNode.next = curr;
prev.next = newNode;

Answer 2 : Correct Option is Option(C) ie. firstNode = firstNode.next;

If curr refers to the second node in the list and the head of the list is the reference variable fistNode.

So, the statement that  deletes the first node of a linked list implementation of ListInterface that has 10 nodes are :

firstNode = firstNode.next;

Answer 3 : Correct Option is Option(C) ie. The runtime of remove(1) (removing the first element) is independent of the number of items in LinkedList.

Explanation:

Option(A) is Incorrect. As in he worst-case runtime of contains() is dependent of the number of items in LinkedList. And is directly proportional to the number of items in the Linked List.

Option(B) is Incorrect. The runtime of getLength() is dependent of the number of items in LinkedList. As it is depends upon the total numbers of items in the LinkedList.

Option(C) is Correct. The runtime of remove(1) (removing the first element) is independent of the number of items in LinkedList. It will take constant time to remove first element irrespective of the size of LinkedList.

Option(D) hence are Incorrect.

Option(E) hence are Incorrect,

Answer 4 : Correct Answer is Option(E) ie. The list is Empty.

Explanation :

Option(A) is Incorrect. the list is not full.

Option(B) is Incorrect. It doesnot mean the garbage collector should be invoked

Option(C) is Incorrect. It doesnot mean that we cannot add items to the list.

Optioon(D) is Incorrect. It doesn't mean the list is in an unstable state

Option(E) is Correct. That it means the list is empty.

Hence,

Option(E) is the correct Option ie. the list is empty.


Related Solutions

Suppose a linked list of 20 nodes. The middle node has a data –250. Write the...
Suppose a linked list of 20 nodes. The middle node has a data –250. Write the pseudocode to replace the middle node of the linked list with a new node and new data. Assume that the list's head pointer is called head_ptr and the data for the new node is called entry
Suppose a linked list of 20 nodes. The middle node has a data –250. Write the...
Suppose a linked list of 20 nodes. The middle node has a data –250. Write the pseudocode to replace the middle node of the linked list with a new node and new data. Assume that the list's head pointer is called head_ptr and the data for the new node is called entry
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...
JAVA How to make a graph class that uses a linked list class to store nodes...
JAVA How to make a graph class that uses a linked list class to store nodes and linked list within each node to store adjacency list The linked list class has been made already.   import java.util.*; public class LinkedList implements Iterable { private int size = 0; private Node head; private Node tail; private class Node { private T data; private Node prev; private Node next; public Node(T data) { this.data = data; } }    public Iterator iterator() {...
Find the last node of a linked list of n elements whose index is a multiple...
Find the last node of a linked list of n elements whose index is a multiple of k (counted from 0). For example, if the list is 12 → 75 → 37 → 99 → 12 → 38 → 99 → 60 ↓ and k = 4, then you should return the second 12 (with index 4). Your algorithm should take O(n) time and use O(1) extra space. Implement the following method in LinkedList.java. public T lastK(int k) LinkedList.java. public...
3. Find the last node of a linked list of n elements whose index is a...
3. Find the last node of a linked list of n elements whose index is a multiple of k (counted from 0). For example, if the list is 12 → 75 → 37 → 99 → 12 → 38 → 99 → 60 ↓ and k = 4, then it should return the second 12 (with index 4). The algorithm should take O(n) time and use O(1) extra space. Implement the following method in LinkedList.java. public T lastK(int k)
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...
//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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT