1. Linked Lists
An Alternative Collections Data
Structure
What Is A Linked List?
A data structure using memory that
expands and shrinks as needed
Relies on identical nodes “pointing” or
“linked” to other nodes
Each node is an instance of a class (i.e. an
object)
Head
Example Declarations
public class IntNode
{
private int data;
private IntNode link;
…
}
OR
public class Part
{
private int partNo;
private double price;
private Part link;
…
}
2. Special Nodes
Head node
The first node in a linked list
Almost all programs using linked lists will
maintain a pointer to the head node
An empty list is represented by storing Null in the
head variable
Tail node
The last node in a linked list
Many programs maintain a pointer to the tail
node
A tail node points to a “null” reference
In an empty list tail will also be Null
Necessary Methods
Methods to access and modify data:
e.g. getData(), getLink(), setData(int
newData), setLink(??? newLink)
Methods for creating nodes
i.e. constructors
Methods for adding and removing
nodes
Caution!
Always make sure that your linked list
methods work correctly with an empty
list.
3. Adding a Node to the Front
of a List
1. Create a new node with the desired data, and
the link pointing to the current head node
2. Make the “head” pointer point to the new node
What happens if we started with an empty list?
remove
Head
Removing the First Node
of a List
Make the head pointer point to what the
current head node is pointing to
What happens to the original head node?
What if there was only one node in the list?
remove
Head
Adding a Node After a
Selected Node
1. Create a new node pointing to whatever the
selected node is currently pointing to
2. Make the selected node point to the new node
What happens if the selected node was the last node
in the list?
remove
Head Selected
4. Removing a Selected Node
Head
Selected
Can we remove this?
Removing the Node After
a Selected Node
Head
Selected
Can we remove this?
Desired Result:
Head
Selected
What happens if we removed the tail node?
Manipulating Entire Linked
Lists
Traverse the entire list in a loop that
terminates when the null pointer is
reached
In the body of the loop perform the
necessary operation(s)
5. Manipulating Entire Linked
Lists
Examples:
count the elements of the list
Print the elements of the list
Update the values in the list (e.g. increase
all prices by 10%)
If implemented in the same class as the
node (i.e. no separate list class), it is better
to use static methods, this way the method
can be used for empty lists
Sample Code
IntNode current;
for (current = head; current != null; current = current.link)
{
…
}
Or:
IntNode current;
current = head;
while (current != null)
{
…
current = current.link;
}
Copying an Entire List
public static IntNode copyList (IntNode head)
{
if (head == null) return null;
IntNode copyHead = new IntNode (head.data,null);
IntNode copyTail = copyHead;
IntNode source = head;
while (source.link != null)
{
source = source.link;
copyTail.addNodeAfter(source.data);
copyTail = copyTail.link;
}
return copyHead;
}
6. Copying a Part of a List
Given a start node and an end node
make a copy of that part of the list
Can we return both the head and tail
of the copy?
How does the method work? Is it that
much different from copying the full
list?
Linked List Application –
IntLinkedBag
Keeps track of integer elements
Can add or delete elements
Answers questions about its elements
Can add all the contents of another
bag
Grows and shrinks in size as needed
IntLinkedBag - Methods
public void add (int element)
public void addAll (IntLinkedBag addend)
public int countOccurences(int target)
public boolean remove (int target)
public int size()
public void trimToSize()
public static IntLinkedBag union (IntLinkedBag b1,
IntLinkedBag b2)
public int grab()
public void ensureCapacity (int minimumCapacity) ???
public int getCapacity() ???
7. Another Linked List Application
- DoubleLinkedSeq
Similar to bag but sequence is preserved
An additional instance variable is needed to hold
the position of the “current” element
Two additional instance variables “tail” and
“precursor” may be used for convenience
Some methods can be preserved, others must be
modified
Additional methods are needed
Implementation: assignment 5
Doubly Linked Lists
Similar to linked lists with pointers in
both directions
Head Tail
Array vs. Linked Lists vs.
Doubly Linked Lists
Which is better for the following
operations?:
Random access
Frequent additions/removals in the
middle of a sequence
Frequent forward and backward
searching of a sequence
Frequent size changes