Question

Complete two of the List methods: SinglyLinkedList::size() and SinglyLinkedList::get(). import java.util.AbstractList; public class SinglyLinkedList<E> extends AbstractList<E>...

Complete two of the List methods: SinglyLinkedList::size() and SinglyLinkedList::get().


import java.util.AbstractList;


public class SinglyLinkedList<E> extends AbstractList<E> {

  /** Reference to the first node in our linked list. This will be null if the list is empty. */
  private Entry head;

  /**
   * Creates an empty list.
   */
  public SinglyLinkedList() {
    reset();
  }

  /**
   * This method, which is only used within the SinglyLinkedList class, returns the instance to its initial state. This
   * call will reset the head to be null, which also empties the linked list.
   */
  private void reset() {
    head = null;
  }

  /**
   * Returns the element at the specified index in this list.
   *
   * @param index List location whose element should be returned.
   * @return Element at the specified index in this list
   */
  @Override
  public E get(int index) {

  }

  /**
   * Returns the number of elements currently in this list.
   *
   * @return the number of elements in the list
   */
  @Override
  public int size() {

  }

  /**
   * Class which defines the Entry instances ("nodes") in this single-linked-based list. Note that this class does not
   * specify a generic type, because it MUST be identical to the element type of the main class.
   *
   * @author Matthew Hertz
   */
  private class Entry {
    /** Element stored with the current entry. */
    private E element;

    /** Reference to the next entry in our linked list or null if this is the final link. */
    private Entry next;

    /** Create a new, blank Entry. */
    public Entry() {
      element = null;
      next = null;
    }
  }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find my implementation.

package year_2017.october.thirdweek;

import java.util.AbstractList;

public class SinglyLinkedList<E> extends AbstractList<E> {

   /** Reference to the first node in our linked list. This will be null if the list is empty. */

   private Entry head;

   /**

   * Creates an empty list.

   */

   public SinglyLinkedList() {

       reset();

   }

   /**

   * This method, which is only used within the SinglyLinkedList class, returns the instance to its initial state. This

   * call will reset the head to be null, which also empties the linked list.

   */

   private void reset() {

       head = null;

   }

   /**

   * Returns the element at the specified index in this list.

   *

   * @param index List location whose element should be returned.

   * @return Element at the specified index in this list

   */

   @Override

   public E get(int index) {

       if(index < 0 || index >= size())

           return null;

      

       int i = 0;

       Entry temp = head;

       while(i < index) {

           i++;

           temp = temp.next;

       }

      

       return temp.element;

   }

   /**

   * Returns the number of elements currently in this list.

   *

   * @return the number of elements in the list

   */

   @Override

   public int size() {

       int i = 0;

       Entry temp = head;

       while(temp != null) {

           i++;

           temp = temp.next;

       }

       return i;

   }

   /**

   * Class which defines the Entry instances ("nodes") in this single-linked-based list. Note that this class does not

   * specify a generic type, because it MUST be identical to the element type of the main class.

   *

   * @author Matthew Hertz

   */

   private class Entry {

       /** Element stored with the current entry. */

       private E element;

       /** Reference to the next entry in our linked list or null if this is the final link. */

       private Entry next;

       /** Create a new, blank Entry. */

       public Entry() {

           element = null;

           next = null;

       }

   }

}

Add a comment
Know the answer?
Add Answer to:
Complete two of the List methods: SinglyLinkedList::size() and SinglyLinkedList::get(). import java.util.AbstractList; public class SinglyLinkedList<E> extends AbstractList<E>...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT