Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Linked Lists Slides

Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

Linked Lists

Robert Horvick
SOFTWARE ENGINEER

@bubbafat www.roberthorvick.com
What are linked lists?
Overview - Nodes
- Node chains

Singly linked lists


Doubly linked lists
Sorted linked lists
Demo: Updating the contact manager
Linked List
A container where data is stored in nodes consisting of a
single value item and a reference to the next node.
Similar to a chain
Start at the first link
Follow the chain to
the last link
The Node

value value value value

next next next next


class Node
{
public Node(int value)
{
this.Value = value; Value
this.Next = null;
}

public Node Next; Next


public int Value;
}
Node head = new Node(1);

Connecting Nodes into a List

1
null
Node head = new Node(1);

head.Next = new Node(2);

Connecting Nodes into a List

1 2
next null
Node head = new Node(1);

head.Next = new Node(2);

head.Next.Next = new Node(3);

Connecting Nodes into a List

1 2 3
next next null
Singly Linked List
A linked list that provides forward iteration from the
start to the end of the list.
class LinkedListNode<TNode> {

public LinkedListNode(TNode value, LinkedListNode<TNode> next = null) {


this.Value = value;
this.Next = next;
}

public LinkedListNode<TNode> Next;


public TNode Value;
}

Singly Linked List Node


A generic class containing the data and reference to the next node
Doubly Linked List
A linked list that provides forward iteration from the
start to the end of the list, and reverse iteration, from
end to start.
class Node
{
public Node(int value)
{ Value
this.Value = value;
this.Previous = null;
this.Next = null;
} Previous
public Node Previous;
public Node Next;
Next
public int Value;
}
Node node1 = new Node(1);

Node node2 = new Node(2);

Node node2 = new Node(3);

Connecting Doubly Linked Nodes Into a List


1 2 3
null null null
null null null
node1.Next = node2;

node2.Previous = node1;

Connecting Doubly Linked Nodes Into a List


1 2 3
null prev null
next null null
node2.Next = node3;

node3.Previous = node2;

Connecting Doubly Linked Nodes Into a List


1 2 3
null prev prev
next next null
Doubly Linked List Node
class DoublyLinkedListNode<TNode>
{
public DoublyLinkedListNode(TNode value,
Node<TNode> prev = null,
Node<TNode> next = null) {
this.Value = value;
this.Previous = prev;
this.Next = next;
}

public Node<TNode> Previous;


public Node<TNode> Next;
public TNode Value;
}
Construction
public class DoublyLinkedList<T> : IEnumerable<T>
{
Node<T> head = null;
Node<T> tail = null;

public int Count


{
get;
private set;
}
}

Doubly Linked List Class


A generic class containing references to the first (head) and last (tail) nodes
Adding Items
Adding Items

Function Behavior Complexity


AddHead Adds a value to the O(1)
beginning of the list
AddTail Adds a value at the end of O(1)
the linked list
DoublyLinkedList<int> ints = new DoublyLinkedList<int>();

for (int i = 1; i <= 5; i++)

ints.AddHead(i);

Adding Values Using AddHead


1
null
null
DoublyLinkedList<int> ints = new DoublyLinkedList<int>();

for (int i = 1; i <= 5; i++)

ints.AddHead(i);

Adding Values Using AddHead


2 1
null prev
next null
DoublyLinkedList<int> ints = new DoublyLinkedList<int>();

for (int i = 1; i <= 5; i++)

ints.AddHead(i);

Adding Values Using AddHead


3 2 1
null prev prev
next next null
DoublyLinkedList<int> ints = new DoublyLinkedList<int>();

for (int i = 1; i <= 5; i++)

ints.AddHead(i);

Adding Values Using AddHead


4 3 2 1
null prev prev prev
next next next null
DoublyLinkedList<int> ints = new DoublyLinkedList<int>();

for (int i = 1; i <= 5; i++)

ints.AddHead(i);

Adding Values Using AddHead


5 4 3 2 1
null prev prev prev prev
next next next next null
27

public void AddHead(T value)  Adding a value to the head (start) of the list
{
 Allocate the new node
DoublyLinkedListNode<T> adding = new DoublyLinkedListNode
<T>(value, null, head);

if(head != null)  If there was an existing head node


{
 Update its previous pointer to the new node
head.Previous = adding;
}

head = adding;  Set the head pointer to the new node

if (tail == null)  If the list was empty (tail is null)


{
tail = head;  Then the head and tail are the same

Count++;  Increment the Count value (items in the list)


}
DoublyLinkedList<int> ints = new DoublyLinkedList<int>();

for (int i = 1; i <= 5; i++)

ints.AddTail(i);

Adding Values Using AddTail


1
null
null
DoublyLinkedList<int> ints = new DoublyLinkedList<int>();

for (int i = 1; i <= 5; i++)

ints.AddTail(i);

Adding Values Using AddTail


1 2
null prev
next null
DoublyLinkedList<int> ints = new DoublyLinkedList<int>();

for (int i = 1; i <= 5; i++)

ints.AddTail(i);

Adding Values Using AddTail


1 2 3
null prev prev
next next null
DoublyLinkedList<int> ints = new DoublyLinkedList<int>();

for (int i = 1; i <= 5; i++)

ints.AddTail(i);

Adding Values Using AddTail


1 2 3 4
null prev prev prev
next next next null
DoublyLinkedList<int> ints = new DoublyLinkedList<int>();

for (int i = 1; i <= 5; i++)

ints.AddTail(i);

Adding Values Using AddTail


1 2 3 4 5
null prev prev prev prev
next next next next null
33

public void AddTail(T value)  Adding a value to the tail (end) of the list
{
if (tail == null)  If the list is empty defer to AddHead
{
AddHead(value);
}
else
{
DoublyLinkedListNode<T> adding = new  Allocate the node being added
DoublyLinkedListNode<T>(value, tail);

tail.Next = adding;  Point the current tail’s next to the new node

tail = adding;  Set the list tail to the new node

Count++;
 Increment the Count value (items in the list)
}
}
Finding Items
Finding Items

Function Behavior Complexity


Find Finds the first node O(n)
whose value equals the
provided argument
Contains Returns true if the O(n)
specified value exists in
the list, false otherwise
private LinkedListNode<T> Find(T value)
{
LinkedListNode<T> current = head;

while(current != null) {
if(current.Value.Equals(value))
return current;

current = current.Next;
}

return null;
}

Finding Values in the List


1 2 3 4 5
public bool Contains(T value)
{
return Find(value) != null;
}

Determine if the List Contains a Value


Return true if the value is found, false otherwise
Removing Items
Removing Items

Function Behavior Complexity


Remove Removes the first node O(n)
whose value is equal to the
argument
Removing a Node

1 2 3

next next null


null prev prev
Removing a Node

1 3

? null
null ?
Removing a Node

1 3

next null
null prev
Remove algorithm
Demo - Find the node to delete
- Remove the found node

Three Cases
- Empty list
- Single node
- Multiple nodes
Enumeration
Enumeration

GetEnumerator()

1 2 3 4 5

1 2 3 4 5

GetReverseEnumerator()
LinkedList<int> ints = LinkedList<int>();  Create a list of integers

ints.AddTail(1);  Build the list with the values 1->2->3


ints.AddTail(2);

ints.AddTail(3);

 Enumerate from the head to the tail


foreach(int value in ints)

Console.WriteLine(value);  Print out the list values (1, 2, 3)

}
LinkedList<int> ints = LinkedList<int>();  Create a list of integers

ints.AddTail(1);  Build the list with the values 1->2->3


ints.AddTail(2);

ints.AddTail(3);

 Enumerate from the tail to the head


foreach(int value in

ints.GetReverseEnumerator())

Console.WriteLine(value);  Print out the list values (3, 2, 1)

}
Sorted List
A doubly linked list where the values are inserted and
stored in sort-order.
Adding Sorted Items

Function Behavior Complexity


Add Adds the specified item to O(n)
the linked list in the sort
order of the item type.
SortedList<int> sorted = new SortedList<int>();

sorted.Add(3);

sorted.Add(2);

sorted.Add(5);

sorted.Add(4);

sorted.Add(1);

Inserting Values into a Sorted List


3
SortedList<int> sorted = new SortedList<int>();

sorted.Add(3);

sorted.Add(2);

sorted.Add(5);

sorted.Add(4);

sorted.Add(1);

Inserting Values into a Sorted List


2 3
SortedList<int> sorted = new SortedList<int>();

sorted.Add(3);

sorted.Add(2);

sorted.Add(5);

sorted.Add(4);

sorted.Add(1);

Inserting Values into a Sorted List


2 3 5
SortedList<int> sorted = new SortedList<int>();

sorted.Add(3);

sorted.Add(2);

sorted.Add(5);

sorted.Add(4);

sorted.Add(1);

Inserting Values into a Sorted List


2 3 4 5
SortedList<int> sorted = new SortedList<int>();

sorted.Add(3);

sorted.Add(2);

sorted.Add(5);

sorted.Add(4);

sorted.Add(1);

Inserting Values into a Sorted List


1 2 3 4 5
Adding Sorted Items Algorithm

Add Empty? Yes Add at head

No

< Head? Yes

No

> Tail? Yes Add at tail

No

Find Insert
Insert
Point
Demo Replace the Contact Manager storage
array with a sorted list
- Removes size limit
- Contacts are implicitly sorted
- Code is simplified by not managing
the array storage
Lists are nodes chains together
Operations
- Adding and removing
- Enumeration
- Searching

Demo: Contact Manager


- Removed size limitation
- Simplified code

You might also like