Package MediaPlayer
Class LinkedList<E>
- java.lang.Object
-
- MediaPlayer.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
-
Field Summary
Fields Modifier and Type Field Description protected LinkedList.Node
head
protected int
size
-
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 indexvoid
addFirst(E element)
Adds an element to the beginning of the listvoid
addLast(E element)
Adds an element to the end of the listvoid
clear()
Clears the list, removing all elementsboolean
contains(E element)
Checks if the list contains the specified elementE
get(int index)
Returns the element at the specified index without removing itE
getFirst()
Returns the first element in the list without removing itE
getLast()
Returns the last element in the list without removing itint
indexOf(E element)
Returns the index of the first occurrence of the specified elementboolean
isEmpty()
Checks if the list is emptyjava.util.Iterator<E>
iterator()
E
remove(int index)
Removes and returns the element at the specified indexE
removeFirst()
Removes and returns the first element in the listE
removeLast()
Removes and returns the last element in the listvoid
reverse()
Extra challenge: Reverses the list in placeint
size()
Returns the number of elements in the listjava.lang.String
toString()
Returns a string representation of the list
-
-
-
Field Detail
-
head
protected LinkedList.Node head
-
size
protected int size
-
-
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 elementelement
- 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 classjava.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
-
-