Linked List Structure
Linked List Structure
Linked List Structure
LINKED LISTS
LINKED LIST STRUCTURE
FIRST NULL
The last node has a pointer to null. If you ever get here, then
you know you are at the end of the Linked List
Adding a new node to the beginning of the list is very common (ie. Stacks).
To add N, first we must make it point to first. This is done by making
N.next = FIRST.
Finally, we must make N the first node. This is done by making N = FIRST.
FIRST
N NULL
DELETING A NODE
FIRST N
Notice N still has
an N.next pointer.
Warmth
If we want to delete N, we need to
make the node before it point to
However, since
footwear nothing is
N.next
referencing N, it
This means you must be able to
will be picked up
access the previous node. (Make a
by garbage
new node)
collection.
FIRST N If no node
remembers you,
you don't exist!
FIRST K-1
ITERATION The Holy Trinity of Iteration
Unlike Arrays, Linked Lists do not have a built-in
Initialization
inspirational
length function sayings
When looping, you typically start at first, much Run Condition
like index 0 in arrays
Instead of being bounded by the length, looping
continues until you reach NULL Incrementer
Instead of adding 1 to the index, you move on
the next node
For(Node n = first; n != null; n = n.next)
PREV NODES
FOR EXAMPLE
while(currNode != null) {
Sometimes you'll need to
if(something)
keep track of the previous
then something;
node.
prevNode = currNode;
When iterating, either
currNode = currNode.next;
through a while or for
}
loop,make sure to update
it at the end of the loop
and prior to incrementing Prev Curr
your current node.
If you increment current When your loop repeats, it should
first, both prev and current look like this:
will point to the same
node!
Two common uses of Linked Lists are for implementing Stacks and
S
Queues. Both are simply collections of items that operate under
certain rules. FIRST
1 2 3 4
T
Much like a stack of
plates, stacks operate In a Linked List
under the Last-In- FIRST
implementation of a
A
First-Out policy. This Stack, the top of the 1
means that when an stack is represented
item is added, it goes by First.
C
at the front-- where it 2
will then be the first Adding a new element
thing to be removed. is done with the
K
push(item) function
Removing a plate 3
from the bottom of the Removing is done
stack would not be a with the pop()
S
good move. function. This function
returns the element
4
removed.
Queues
LINKED LISTS TRADEOFFS
ARRAYS VS. LINKED LISTS
ARRAYS LINKED LISTS
+ Instant access to
+ No size limit. Can add
elements through
nodes as long as there is
[indexing]
memory space available
- Size is immutable.
+ Good for continuously
When initializing an
growing data
array, a size must be
given and cannot be
- No indexing, so no instant
changed. Requires
access.
rezising.
RUN TIME
Basics
Computers work one operation at a time
Consider having an input size of N for a function that
prints out all the elements in an array. The computer
will do the print operation the same amount of times
as there are elements
Thus, the run time would be N
BIG-O COMPLEXITY
SORTING
Selection Sort
Find the smallest item in the array, and
exchange it with the first entry
Find the next smallest item and exchange it
with the second entry
Continue until the entire array is sorted
Repeatedly selects the smallest remaining item
N^2/2 compares and N exchanges to sort
an array of length N
Insertion Sort
Compares the item before it and continues
to swap backwards if the item is greater
N^2/4 compares and N^2/4 exchanges on
average
The worst case is N^2/2 compares and
N^2/2 exchanges and the best case is N-1
compares and 0 exchanges.
Merge Sort
Divide array into two halves until only single items
remain
Sort the two halves recursively
Then merge the results.
Between 1/2 N lg N and N lg N compares and at
most 6 N lg N array accesses to sort any array of
length N.
0
UNiONS
Connected components are simply
the items that are either connected or
1 8 2 7 4 alone. In this case, there are 4
connected components.
Depending on the implementation,
these components will be stored in an
6 5 9 3 array and will be identified by the
parent element
Quick-Find
IMPLEMENTATION & API
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
4 4 | 4 | 4 | 6 | 4 | 4 | 6 | 6 | 4 | 6 | 10
1 5 Notice that every value of 5, the old root, was
8 changed to 2, the new root.
The Union function would be written like this:
0 5 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
1 | 4 | 5 | 6 | 4 | 5 | 6 | 6 | 4 | 7 | 10
10 2
Because each site holds the value of its parent and not
the root, implementing the find() function will require more
work, since we will have to essentially "climb" up the public int find(int p) {
component tree until we find the root. In the case of while (p != parent[p])
find(0) for example, we would first have to go to 1, and p = parent[p];
then to 4 before returning the value. This extra work is return p;
reflected through the use of a loop in the code. }
Observe how find(0) would run on our array:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
1 | 4 | 5 | 6 | 4 | 5 | 6 | 6 | 4 | 7 | 10
4
1 5 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
8 1 | 4 | 5 | 6 | 4 | 4 | 6 | 6 | 4 | 7 | 10
0 2
Weighted Quick-Union
Although Quick-Union is a helpful To solve this issue, we create a new array that holds the
implementation, it can lead to a sizes of each tree and compares them when connecting
two components. In the case of union(4,5), the program
tree increasing in depth after
would compare the sizes of 4 and 5, and upon determining
connecting two structures. See the that 4 was the larger of the two, it would make that the
case of union(4,5) for example: parent. The only difference in code will come in the union()
function.
5
public void union(int p, int q) {
int rootP = find(p);
4 2 int rootQ = find(q);
4 if (rootP == rootQ) return;
if (size[rootP] < size[rootQ]) {
1 8 1 5 parent[rootP] = rootQ;
8 size[rootQ] += size[rootP]; }
else {
2 parent[rootQ] = rootP;
0 0 size[rootP] += size[rootQ]; }
}