LinkedLists Flashcards
(34 cards)
What are the main limitations of an ArrayList that may provoke using a LinkedList?
- There’s a fixed number of elements in the underlying array - Although the ArrayList class provides functionality to grow the underlying array, it can be time consuming and thus introduce latency.
- You cannot easily insert an element in between others - When this is done all of the other elements in the ArrayList are shifted so you lose track of the original position.
- Searching for a value can be very slow
What is a LinkedList?
A data structure that stores elements in arbitrary, noncontiguous memory locations to facilitate efficient insertion and removal, but also provides additional information to link the elements together. The element plus the link is referred to as a node. The first node in the list is referred to as the head and the last node in the list is known as the tail. Note that the tail links to “null”.
What is the Node class?
A LinkedList stores elements in a sequence of nodes, which are represented by the Node class. Because the methods of the LinkedList class and the Iterator class have frequent access to the Node instance variables, we do not make the instance variables of the Node class private. Instead, we make Node a private inner class of the LinkedList class. The class definition is as follows:
public class LinkedList<e> {<br></br> class Node {</e>
//The data stored in the node
E value;
//Node object that is a link to the next node in the list
Node next;
//This is the constructor
Node(E value) {
this.value = value;
}
}
//References to the first and last nodes in the list
protected Node head = null;
protected Node tail = null;
}
How do you add an element to the beginning of a LinkedList?
You need to do three things: First, create the new node. Next, set it’s link to the node that will come after it, which in this case is the head. Finally, update the head of the LinkedList. You just need to make sure you handle the situation in which the list was previously empty so this new node is the head and the tail. This additional code is circled below:

How do you add a new element to the end of a LinkedList?
Similar to adding a new node to the beginning of the LinkedList, there are three steps to doing this. First, you create the new node. Then you set the next node of the tail to refer to the new one. Finally, you update the tail to refer to the new node. You similarly just need to handle situations in which the list was empty and the new node is the head and tail! This is outlined below:

How do you add an element somewhere in the middle of a LinkedList?
You need to traverse, or iterate through, all elements in the list until you reach the point at which you want to insert the new node. Then you need to update the links of the nodes before and after the new one. There are quite a few exceptions that have to be handled in this case. They are demonstrated below:

How do you remove the first element from a LinkedList?
You simply update the head to refer to the second node in the LinkedList. It is not actually removed, but unlinked and eventually it is garbage collected. The code is as follows:

How do you remove the last element from a LinkedList?
You need to do two things. First, you need to update the link of the second to last element so that it points to null. Then update the tail to point to that second to last node. Since we don’t have a direct link to the second to last node, we need to walk through the links in the list until we find it.

How do you remove an element from the middle of a LinkedList?
You walk through all links in the list from the head to the one preceeding the link you want to remove. Then you update it’s link to the node after the one you want to remove so it’s skipped or removed from the list. The code for this is shown below:

How can you get an element from a LinkedList using its index?
You use the get( ) method. The code for this is shows below. Note that as we’ve seen before, you cannot directly access an element in a LinkedList by its index. You need to start at the head and advance n nodes until you reach the one you want to retrieve. The code for this is shown below:

How can you determine if a value is contained in a LinkedList?
You can use the contains( ) method. The code for this is shown below. Note that as with ArrayLists, you cannot instantly determine if a value is contained in a LinkedList because you can only 1 value in the list with the target value at a time so you need to inspect each value starting with the head until we find what the target value.

For a linked list that contains 3 or more elements, if head is a reference to the first node of a LinkedList, what happens when you run the operation head = tail; ?
It removes all nodes from the LinkedList except for the last element.
For what operation is a LinkedList slower than an ArrayList?

Retrieving an element by its index.

- A drive through ordering system
- A waitlist for standby passengers at an airport
- A CPU that is shared among different jobs based upon arrival time
What is a Stack?
An abstract data type that is a specialized version of LinkedList and works like a LinkedList, but only has two operations that allow you to add and remove elements to the head, or top. They are:
- push( ) - adds an element to the head / top
- pop( ) - removes an element from the head / top
As this illustrates, Stacks are therefore a LIFO (last in, first out) data structure. They are also one of the simplest of all data structures, yet among the most important as they are used in a host of different applications and in more complex algorithms.
What is an abstract data type (ADT)?
An abstract data type (ADT) is a model of a data structure that specifies:
- the characteristics of the collection of data
- the operations that can be performed on the collection
It’s abstract because it doesn’t specify how the ADT will be implemented. In Java, we implement these with an interface that specifies the methods but does not define them. Given that an interface serves as a type definition, but cannot be directly instantiated, we must provide one or more concrete classes that implement the ADT.
What are a few common implementations of a Stack data structure?
The name “stack” is derived from the metaphor of a stack of plates in a spring-loaded, cafeteria plate dispenser. In this case, the fundamental operations involve “pushing” and “popping” the plates off of the stack. An even more amusing example is the Pez candy dispenser, which is a physical implementation of a stack.
In software engineering, there are two common implementations of a stack data structure:
- Internet Web Browsers - these store the address of recently visited sites on a stack. Each time a user visits a new site it is “pushed” onto the stack of addresses. The browser then allows the user to “pop” back to the previously visited sites using the “back” button.
- Text Editors - these provide an “undo” mechanism that cancels recent operations and reverts back to a former state of a document. These undo operations can be accomplished by keeping text changes in a stack.
How is a Stack implemented in the Java collections framework?

What is a Queue?
A data structure that is a specialized version of a LinkedList and works like a LinkedList, but only has two operations:
- add( ) - adds an element to the rear / tail
- remove( ) - removes an element from the front / head
Note that a Queue is a FIFO (first in, first out) data structure.

What are a few common implementations of the Queue ADT?
A queue is a logical choice for a data structure to handle calls to a customer service center or managing a waiting list at a restaurant. They’re also used by computing devices such as networked printers and web servers that respond to requests.
How is a Queue implemented in the Java collections framework?
This data structure is implemented as an interface, not a Class, and therefore cannot be instantiated. Therefore, when a Queue is declared it is done as type Queue but instantiated as a LinkedList (this is possible because the LinkedList class implements the Queue interface). For example: *protected Queue<integer> ticketRequests = new LinkedList<interger>( );</interger></integer>*

How can you determine if a Stack or Queue is empty?
- empty( ) - tests if a Stack is empty
- isEmpty( ) - tests if a Queue is empty
How do you remove and return the node at the head of a Stack and a Queue?
- Stack.pop( )
- Queue.remove( )
How do you return the node at the head of a Stack or Queue without removing it?
- Stack.peek( )
- Queue.peek( )




