Package MediaPlayer

Class LinkedList<E>

  • All Implemented Interfaces:
    java.lang.Iterable<E>
    Direct Known Subclasses:
    BetterLinkedList

    public class LinkedList<E>
    extends java.lang.Object
    implements java.lang.Iterable<E>
    LinkedList class A implementation of a singly linked list data structure with a head pointer.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected class  LinkedList.Node
      Node class for the LinkedList Contains data and a reference to the next node
    • Constructor Summary

      Constructors 
      Constructor Description
      LinkedList()
      Default constructor for the LinkedList class Creates an empty list
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int index, E element)
      Adds an element at the specified index
      void addFirst​(E element)
      Adds an element to the beginning of the list
      void addLast​(E element)
      Adds an element to the end of the list
      void clear()
      Clears the list, removing all elements
      boolean contains​(E element)
      Checks if the list contains the specified element
      E get​(int index)
      Returns the element at the specified index without removing it
      E getFirst()
      Returns the first element in the list without removing it
      E getLast()
      Returns the last element in the list without removing it
      int indexOf​(E element)
      Returns the index of the first occurrence of the specified element
      boolean isEmpty()
      Checks if the list is empty
      java.util.Iterator<E> iterator()  
      E remove​(int index)
      Removes and returns the element at the specified index
      E removeFirst()
      Removes and returns the first element in the list
      E removeLast()
      Removes and returns the last element in the list
      void reverse()
      Extra challenge: Reverses the list in place
      int size()
      Returns the number of elements in the list
      java.lang.String toString()
      Returns a string representation of the list
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • LinkedList

        public LinkedList()
        Default constructor for the LinkedList class Creates an empty list
    • Method Detail

      • size

        public int size()
        Returns the number of elements in the list
        Returns:
        The number of elements in the list
      • isEmpty

        public boolean isEmpty()
        Checks if the list is empty
        Returns:
        true if the list is empty, false otherwise
      • getFirst

        public E getFirst()
        Returns the first element in the list without removing it
        Returns:
        The first element in the list
        Throws:
        java.util.NoSuchElementException - if the list is empty
      • getLast

        public E getLast()
        Returns the last element in the list without removing it
        Returns:
        The last element in the list
        Throws:
        java.util.NoSuchElementException - if the list is empty
      • addFirst

        public void addFirst​(E element)
        Adds an element to the beginning of the list
        Parameters:
        element - The element to add
      • addLast

        public void addLast​(E element)
        Adds an element to the end of the list
        Parameters:
        element - The element to add
      • removeFirst

        public E removeFirst()
        Removes and returns the first element in the list
        Returns:
        The first element in the list
        Throws:
        java.util.NoSuchElementException - if the list is empty
      • removeLast

        public E removeLast()
        Removes and returns the last element in the list
        Returns:
        The last element in the list
        Throws:
        java.util.NoSuchElementException - if the list is empty
      • add

        public void add​(int index,
                        E element)
        Adds an element at the specified index
        Parameters:
        index - The index at which to add the element
        element - The element to add
        Throws:
        java.lang.IndexOutOfBoundsException - if the index is out of range
      • get

        public E get​(int index)
        Returns the element at the specified index without removing it
        Parameters:
        index - The index of the element to return
        Returns:
        The element at the specified index
        Throws:
        java.lang.IndexOutOfBoundsException - if the index is out of range
      • remove

        public E remove​(int index)
        Removes and returns the element at the specified index
        Parameters:
        index - The index of the element to remove
        Returns:
        The element at the specified index
        Throws:
        java.lang.IndexOutOfBoundsException - if the index is out of range
      • indexOf

        public int indexOf​(E element)
        Returns the index of the first occurrence of the specified element
        Parameters:
        element - The element to search for
        Returns:
        The index of the first occurrence of the element, or -1 if not found
      • contains

        public boolean contains​(E element)
        Checks if the list contains the specified element
        Parameters:
        element - The element to search for
        Returns:
        true if the list contains the element, false otherwise
      • toString

        public java.lang.String toString()
        Returns a string representation of the list
        Overrides:
        toString in class java.lang.Object
        Returns:
        A string representation of the list
      • clear

        public void clear()
        Clears the list, removing all elements
      • reverse

        public void reverse()
        Extra challenge: Reverses the list in place
      • iterator

        public java.util.Iterator<E> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<E>