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

Question 5: True or False: the public void add(E e)method always adds the new it

ID: 3858903 • Letter: Q

Question

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.

***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<E> x) {

        // assert x != null;

        final E element = x.item;

        final Node<E> next = x.next;

        final Node<E> 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

     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.

     * @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;

    }

Explanation / Answer

Question 5: True or False: the public void add(E e)method always adds the new item at the beginning of the linked list.

True : It always add at the front
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.
False: As its adds at the beginning it wont take much time

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?

if case will be execute because, It is the first node and its prev will be null.

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

True: Because it scans the list and checks every element, If the list grows bigger more elements needs to be checked, So Yeah it takes longer to execute the more items there are in the linked list.

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