Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

based on the code below, answer the questions Question 1: The LinkedList class uses another class called Node (which is defined inside LinkedList.java). What are

based on the code below, answer the questions

Question 1: The LinkedList class uses another class called Node (which is defined inside LinkedList.java). What are the fields in the Node class?

Question 2: The Node class uses generics. Why?

Question 3: The linkFirst method contains the following lines of code: if (f == null) last = newNode; else f.prev = newNode; What is the value of the linked lists size attribute at this point in the code if f == null is true?

Question 4: True or False: the removeLast method will take longer to execute the more items are in the linked list.

Question 5: True or False: the public void add(E e)method always adds the new item at the beginning of the linked list. Question 6: True or False: the public void add(E e) method will take longer to execute the more items are in the linked list.

Question 7: The unlink method contains the following lines of code: if (prev == null) { first = next; } else { prev.next = next; x.prev = null; } If the method is called with the first Node in the list as the parameter value, which of these will be executed: the if clause or the else clause?

Question 8: True or false: in general, the contains method takes longer to execute the more items there are in the linked list.

***linkedlistclass ****-question1and 2

public class LinkedList

extends AbstractSequentialList

implements List, Deque, Cloneable, java.io.Serializable

{

transient int size = 0;

/**

* Pointer to first node.

* Invariant: (first == null && last == null) ||

* (first.prev == null && first.item != null)

*/

transient Node first;

/**

* Pointer to last node.

* Invariant: (first == null && last == null) ||

* (last.next == null && last.item != null)

*/

transient Node last;

/**

* Constructs an empty list.

*/

public LinkedList() {

}

/**

* Constructs a list containing the elements of the specified

* collection, in the order they are returned by the collection's

* iterator.

*

* @param c the collection whose elements are to be placed into this list

* @throws NullPointerException if the specified collection is null

*/

public LinkedList(Collection c) {

this();

addAll(c);

}

**********end of code for question 1*************8

**linkFirst method*** (question 3)

private void linkFirst(E e) {

final Node f = first;

final Node newNode = new Node<>(null, e, f);

first = newNode;

if (f == null)

last = newNode;

else

f.prev = newNode;

size++;

modCount++;

} ****end of linkFirst method************

**removeLast method****(question 4)

public E removeLast() {

final Node l = last;

if (l == null)

throw new NoSuchElementException();

return unlinkLast(l);

}

end of remove last method***********

****public void add (E e) method ****(question 5 and 6)

public void add(E e) {

checkForComodification();

lastReturned = null;

if (next == null)

linkLast(e);

else

linkBefore(e, next);

nextIndex++;

expectedModCount++;

}

****end of public add method****

****unlink method**** question 7

/**

* Unlinks non-null node x.

*/

E unlink(Node x) {

// assert x != null;

final E element = x.item;

final Node next = x.next;

final Node prev = x.prev;

if (prev == null) {

first = next;

} else {

prev.next = next;

x.prev = null;

}

if (next == null) {

last = prev;

} else {

next.prev = prev;

x.next = null;

}

x.item = null;

size--;

modCount++;

return element;

}

***end of unlink methood*****

***contains method*******question 8

/**

* Returns {@code true} if this list cs the specified element.

* More formally, returns {@code true} if and only if this list contains

* at least one element {@code e} such that

* (o==null ? e==null : o.equals(e)).

* @param o element whose presence in this list is to be tested

* @return {@code true} if this list contains the specified element

*/

public boolean contains(Object o) {

return indexOf(o) != -1;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions