Java Data Structures: LinkedList, Stack,
and Queue
LinkedList
add(E e)
Description: Appends the specified element to the end of this list. Time Complexity: O(1).
LinkedList<String> list = new LinkedList<>();
[Link]("Apple");
add(int index, E element)
Description: Inserts the specified element at the specified position in this list. Time
Complexity: O(n).
[Link](1, "Banana");
addFirst(E e)
Description: Inserts the specified element at the beginning of this list. Time Complexity:
O(1).
[Link]("Strawberry");
addLast(E e)
Description: Appends the specified element to the end of this list. Time Complexity: O(1).
[Link]("Cherry");
clear()
Description: Removes all of the elements from this list. Time Complexity: O(n).
[Link]();
contains(Object o)
Description: Returns true if this list contains the specified element. Time Complexity: O(n).
boolean hasApple = [Link]("Apple");
get(int index)
Description: Returns the element at the specified position in this list. Time Complexity:
O(n).
String fruit = [Link](0);
getFirst()
Description: Returns the first element in this list. Time Complexity: O(1).
String first = [Link]();
getLast()
Description: Returns the last element in this list. Time Complexity: O(1).
String last = [Link]();
indexOf(Object o)
Description: Returns the index of the first occurrence of the specified element, or -1 if not
found. Time Complexity: O(n).
int index = [Link]("Apple");
lastIndexOf(Object o)
Description: Returns the index of the last occurrence of the specified element, or -1 if not
found. Time Complexity: O(n).
int lastIndex = [Link]("Apple");
peek()
Description: Retrieves, but does not remove, the head (first element) of this list. Time
Complexity: O(1).
String head = [Link]();
poll()
Description: Retrieves and removes the head (first element) of this list. Time Complexity:
O(1).
String head = [Link]();
pop()
Description: Pops an element from the stack represented by this list. Time Complexity: O(1).
String popped = [Link]();
push(E e)
Description: Pushes an element onto the stack represented by this list. Time Complexity:
O(1).
[Link]("Orange");
remove()
Description: Retrieves and removes the head (first element) of this list. Time Complexity:
O(1).
[Link]();
remove(int index)
Description: Removes the element at the specified position in this list. Time Complexity:
O(n).
[Link](1);
size()
Description: Returns the number of elements in this list. Time Complexity: O(1).
int size = [Link]();
Stack
empty()
Description: Tests if this stack is empty. Time Complexity: O(1).
Stack<Integer> stack = new Stack<>();
boolean isEmpty = [Link]();
peek()
Description: Looks at the object at the top of this stack without removing it. Time
Complexity: O(1).
Integer top = [Link]();
pop()
Description: Removes the object at the top of this stack and returns that object. Time
Complexity: O(1).
Integer popped = [Link]();
push(E item)
Description: Pushes an item onto the top of this stack. Time Complexity: O(1).
[Link](10);
search(Object o)
Description: Returns the number of elements in this stack. Time Complexity: O(1).
int size = [Link]();
size()
Description: Returns the number of elements in this stack. Time Complexity: O(1).
int size = [Link]();
Queue
add(E e)
Description: Inserts the specified element into this queue if possible, otherwise throws an
exception. Time Complexity: O(1).
Queue<String> queue = new LinkedList<>();
[Link]("Task 1");
offer(E e)
Description: Inserts the specified element into this queue if possible. Time Complexity: O(1).
[Link]("Task 2");
remove()
Description: Retrieves and removes the head of this queue, throwing an exception if empty.
Time Complexity: O(1).
String task = [Link]();
poll()
Description: Retrieves and removes the head of this queue, or returns null if empty. Time
Complexity: O(1).
String task = [Link]();
element()
Description: Retrieves, but does not remove, the head of this queue, throwing an exception
if empty. Time Complexity: O(1).
String head = [Link]();
peek()
Description: Retrieves, but does not remove, the head of this queue, or returns null if empty.
Time Complexity: O(1).
String head = [Link]();
size()
Description: Returns the number of elements in this queue. Time Complexity: O(1).
int size = [Link]();