Question

based on the code below, answer the questions Question 1: The LinkedList class uses another class...

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 list’s 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<E>

    extends AbstractSequentialList<E>

    implements List<E>, Deque<E>, 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<E> first;

    /**

     * Pointer to last node.

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

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

     */

    transient Node<E> 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<? extends E> c) {

        this();

        addAll(c);

    }

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

**linkFirst method*** (question 3)

private void linkFirst(E e) {

        final Node<E> f = first;

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

    }

Homework Answers

Answer #1

Here are the solutions for the first 4 problems:

Question 1: The LinkedList class uses another class called Node (which is defined inside
           LinkedList.java). What are the fields in the Node class?
Answer 1: The fields in the Node class include:
               -> item of type E.
               -> next of type Node, which points to the next node.
               -> prev of type Node, which points to the previous node.  
                      
Question 2: The Node class uses generics. Why?
Answer 2:   Generics are used for the Node class, to specify that the node items can be
           of any data type.
          
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 list’s
           size attribute at this point in the code if f == null is true?
Answer 3:   f = null will happen, when the first node pointer itself is pointing to null.
           i.e., there are no nodes in the list. Therefore, the size attribute will be 0.
                      
Question 4: True or False: the removeLast method will take longer to execute the more
           items are in the linked list.
Answer 4:   False. As there is a first, and last pointer, where first points to first node,
           and last points to last node. And the other point is that, every node has a
           next pointer as well as the previous pointer, so, to remove the last node, you
           have to traverse atmost one node, starting from last pointer.
          

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
public class LinkedList {       private Node list;       public LinkedList()       {           list =...
public class LinkedList {       private Node list;       public LinkedList()       {           list = null;       }       public Node getList()       {           return list;       }       . . .//other methods       // insert method printHighEarners here       // insert method lowestPaidEmployeeRec       private class Node       {           public Employee data;           public Node next;           public Node(Employee emp)           {               data = emp;               next = null;           }           public String toString()...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue:...
(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue: public static final class LinkedQueue<T> implements QueueInterface<T> {     private Node<T> firstNode;     private Node<T> lastNode;     public LinkedQueue() {         firstNode = null;         lastNode = null;     }     @Override     public T getFront() {         if (isEmpty()) {             return null;         }         return firstNode.getData();     }     @Override     public boolean isEmpty() {         return firstNode == null;    ...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT