Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8. Here is part of the definition for a LinkedList class. class LinkedList priva

ID: 3600472 • Letter: 8

Question

8. Here is part of the definition for a LinkedList class. class LinkedList private ListNode head; private int size; public LinkedList O this, head this.size null; 0; // a private class class ListNode public int item; An item in the list. public ListNode next; // Reference to next item in the list. // LinkedList methods (a) Write a method public void add( int elenent) that adds a new node at the head of the linked list. (Notice that the inner class ListNode only has a default constructor.) (b) Write a method public int remove( ) t removes from the linked list the node at the head of the list and returns the tha int that was stored in that node. Throw an exception if the linked list is empty (e) Explain how you would modify the add method so that the following two lines of code will compile and run correctly LinkedList list new LinkedListO; list.add (3).add (2).add(5, 6, 7).add (O).add (8);

Explanation / Answer

Question: A

public void add(int element)
{
ListNode nptr = new ListNode(element, null);   
size++ ;   
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setLink(start);
start = nptr;
}
}

Question B:

public void remove()
{   
int pos = 1;
if(start == null)
throw new NullPointerException("list is empty");
else{
  
start = start.getLink();
size--;
return ;
}
}

Question: C

For the code to excute in the second line just remove any two numbers because the method add takes only one element.

**Comment for any further queries. Upvote if the answer is satisfactory.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote