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 classLinkedList.NodeNode class for the LinkedList Contains data and a reference to the next node
-
Field Summary
Fields Modifier and Type Field Description protected LinkedList.Nodeheadprotected intsize
-
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 voidadd(int index, E element)Adds an element at the specified indexvoidaddFirst(E element)Adds an element to the beginning of the listvoidaddLast(E element)Adds an element to the end of the listvoidclear()Clears the list, removing all elementsbooleancontains(E element)Checks if the list contains the specified elementEget(int index)Returns the element at the specified index without removing itEgetFirst()Returns the first element in the list without removing itEgetLast()Returns the last element in the list without removing itintindexOf(E element)Returns the index of the first occurrence of the specified elementbooleanisEmpty()Checks if the list is emptyjava.util.Iterator<E>iterator()Eremove(int index)Removes and returns the element at the specified indexEremoveFirst()Removes and returns the first element in the listEremoveLast()Removes and returns the last element in the listvoidreverse()Extra challenge: Reverses the list in placeintsize()Returns the number of elements in the listjava.lang.StringtoString()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:
toStringin 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
-
-