## Circular Linked List—Python

The circular linked list a variant of the singly linked list where the last Node links to the head rather than None. Since the list contains a circular reference to the Head, we can navigate a circular linked list starting at any index rather than starting at the head node.

Here is the code for a circular linked list implementation with unit tests. We won’t dicuss the unit tests in this post, but we will go over the linked list implementation.

```from enum import Enum

class NodeConstants(Enum):
FRONT_NODE = 1

class Node:
def __init__(self, element=None, next_node=None):
self.element = element
self.next_node = next_node

def __str__(self):
if self.element:
return self.element.__str__()
else:
return 'Empty Node'

def __repr__(self):
return self.__str__()

def __init__(self):

def size(self):
count = 0

while current != self.head:
count += 1
current = current.next_node

return count

def insert_front(self, data):
node = Node(element=data, next_node=self.head.next_node)

def insert_last(self, data):

while current_node.next_node != self.head:
current_node = current_node.next_node

node = Node(element=data, next_node=current_node.next_node)
current_node.next_node = node

def insert(self, data, position):
if position == 0:
self.insert_front(data)
elif position == self.size():
self.insert_last(data)
else:
if 0 < position < self.size():
current_pos = 0

while current_pos < position - 1:
current_pos += 1
current_node = current_node.next_node

node = Node(data, current_node.next_node)
current_node.next_node = node
else:
raise IndexError

def remove_first(self):

def remove_last(self):

while current_node.next_node.next_node != self.head:
current_node = current_node.next_node

def remove(self, position):
if position == 0:
self.remove_first()
elif position == self.size():
self.remove_last()
else:
if 0 < position < self.size():
current_pos = 0

while current_pos < position - 1:
current_node = current_node.next_node
current_pos += 1

current_node.next_node = current_node.next_node.next_node
else:
raise IndexError

def fetch(self, position):
if 0 <= position < self.size():
current_pos = 0

while current_pos < position:
current_node = current_node.next_node
current_pos += 1

return current_node.element
else:
raise IndexError

import unittest
from random import randint

names = ['Bob Belcher',
'Linda Belcher',
'Tina Belcher',
'Gene Belcher',
'Louise Belcher']

def test_init(self):
self.assertEqual(dll.size(), 0)

def test_insert_front(self):
for name in TestCircularLinkedList.names:
dll.insert_front(name)

def test_insert_last(self):
for name in TestCircularLinkedList.names:
dll.insert_last(name)

for i in range(len(TestCircularLinkedList.names) - 1):

def test_insert(self):
for name in TestCircularLinkedList.names:
dll.insert_last(name)

pos = randint(0, len(TestCircularLinkedList.names) - 1)

dll.insert('Teddy', pos)
self.assertEqual(dll.fetch(pos), 'Teddy')

def test_remove_first(self):
for name in TestCircularLinkedList.names:
dll.insert_last(name)

for i in range(dll.size(), 0, -1):
self.assertEqual(dll.size(), i)
dll.remove_first()

def test_remove_last(self):
for name in TestCircularLinkedList.names:
dll.insert_last(name)

for i in range(dll.size(), 0, -1):
self.assertEqual(dll.size(), i)
dll.remove_last()

def test_remove(self):
for name in TestCircularLinkedList.names:
dll.insert_last(name)

dll.remove(1)

self.assertEqual(dll.fetch(0), 'Bob Belcher')
self.assertEqual(dll.fetch(1), 'Tina Belcher')
self.assertEqual(dll.fetch(2), 'Gene Belcher')
self.assertEqual(dll.fetch(3), 'Louise Belcher')

if __name__ == '__main__':
unittest.main()
```

## NodeContants

NodeConstants is an example of Python’s enumeration. A circular linked list requires a distinct head node that the client code can easily identify. Without a distinct head node, we could easily introduce an infinate loop when traversing the linked list. We are going to use NodeContants to help identify the head node.

```from enum import Enum

class NodeConstants(Enum):
FRONT_NODE = 1
```

There are other ways to indentify the head node, so using enumerations isn’t required. It does give us a way to show off how to do enumerations in Python for those readers who are interested.

## Node

We can use the same Node class that we used in singular linked list. Like all linked lists, the Node class holds the data stored in the list and a reference to the next Node in the list.

```class Node:
def __init__(self, element=None, next_node=None):
self.element = element
self.next_node = next_node

def __str__(self):
if self.element:
return self.element.__str__()
else:
return 'Empty Node'

def __repr__(self):
return self.__str__()
```

This class is the work house of this module and provides us with the linked list implementation. It’s not very different than the singular linked list implementation.

```class CircularLinkedList:
def __init__(self):

def size(self):
count = 0

while current != self.head:
count += 1
current = current.next_node

return count

def insert_front(self, data):
node = Node(element=data, next_node=self.head.next_node)

def insert_last(self, data):

while current_node.next_node != self.head:
current_node = current_node.next_node

node = Node(element=data, next_node=current_node.next_node)
current_node.next_node = node

def insert(self, data, position):
if position == 0:
self.insert_front(data)
elif position == self.size():
self.insert_last(data)
else:
if 0 < position < self.size():
current_pos = 0

while current_pos < position - 1:
current_pos += 1
current_node = current_node.next_node

node = Node(data, current_node.next_node)
current_node.next_node = node
else:
raise IndexError

def remove_first(self):

def remove_last(self):

while current_node.next_node.next_node != self.head:
current_node = current_node.next_node

def remove(self, position):
if position == 0:
self.remove_first()
elif position == self.size():
self.remove_last()
else:
if 0 < position < self.size():
current_pos = 0

while current_pos < position - 1:
current_node = current_node.next_node
current_pos += 1

current_node.next_node = current_node.next_node.next_node
else:
raise IndexError

def fetch(self, position):
if 0 <= position < self.size():
current_pos = 0

while current_pos < position:
current_node = current_node.next_node
current_pos += 1

return current_node.element
else:
raise IndexError
```

### __init__

We initialize the linked list by creating a head Node and then pointing it’s next_node at itself.

```def __init__(self):
```

In this case, we will use our NodeConstants.FRONT_NODE to help us indentify the head of the list in the debugger. We don’t actually need this but it does help make the code more clear.

### size

This method returns the number of elements contained in the linked list.

```def size(self):
count = 0

while current != self.head:
count += 1
current = current.next_node

return count
```

We begin by making a count variable and a current variable. Current points at self.head.next_node because we aren’t counting self.head. Now we are going to loop until current == self.head. We don’t need to check for None in this case because we don’t have any such Nodes in this implementation.

As we loop, we increment count by one and then advance current to the next node in the list. Eventually, current points at self.head and we terminate the loop at this point. We then return the count.

### insert_front

There isn’t much work to do to insert a Node at the beginning of the list.

```def insert_front(self, data):
node = Node(element=data, next_node=self.head.next_node)
```

We create a new Node and point it’s next node at self.head.next_node. Then we just need to point self.head.next_node at the new Node.

### insert_last

To insert a Node at the end of the list, we need to tranverse the list to right before self.head.

```def insert_last(self, data):

while current_node.next_node != self.head:
current_node = current_node.next_node

node = Node(element=data, next_node=current_node.next_node)
current_node.next_node = node
```

Once again, we have a current_node that requires us to start at self.head.next_node. We then enter a loop that terminates when current_node.next_node == self.head to avoid an infinate loop.

Once we find our insertion point, we create a new Node and point it’s next_node to current_node.next_node (which happens to be self.head). Then current_node.next_node is updated to point at Node.

### insert

The insert method let’s us support insertions in the middle of the list. It works by traversing the list to right before the desired position and performing an insertion.
Keep in mind this method has four possible scenerios it must take into account.

1. Position is 0 -> insert at the front
2. Position == size() -> insert the end
3. Position size() -> throw exception
4. Position > 0 and Position Perform insertion
```def insert(self, data, position):
if position == 0:
# Case 1
self.insert_front(data)
elif position == self.size():
# Case 2
self.insert_last(data)
else:
if 0 < position < self.size():
# Case 4
current_pos = 0

while current_pos < position - 1:
current_pos += 1
current_node = current_node.next_node

node = Node(data, current_node.next_node)
current_node.next_node = node
else:
# Case 3
raise IndexError
```

The cases have been identified with the comments. In cases one and two, we are simply going to reuse code by calling self.insert_front or self.insert_last respectively. We handle case three by raising IndexError to indicate a programming error.

Case four works similar to other other insertions. We start with current_node at self.head.next_node and current_pos at 0. Then we iterate through the list until we reach the node right before the specified position (position – 1).

After exiting the while loop, we create a new Node and point it's next_node at current_node.next_node. The we update current_node.next_node to point at our new Node which now resides at our position.

### remove_first

When removing nodes from the front of the list, we reassign self.head.next_node rather than self.head.

```def remove_first(self):
```

Remember that the last Node in this linked list always points at self.head. If we accidently reassigned self.head rather than self.head.next_node, we would break our linked list. However, when we update self.head.next_node to point at self.head.next_node.next_node, we are removing the Node currently located at self.head.next_node.

The removed Node gets garbage collected by the Python runtime environment and the linked list is shrunk by one element.

### remove_last

It’s a fairly painless process to remove elements from the end of a circular linked list. We simply need to advance to the element located two positions before self.head and then point that Node’s next_node at self.head.

```def remove_last(self):

while current_node.next_node.next_node != self.head:
current_node = current_node.next_node

```

We begin with current_node pointing at self.head.next_node and then enter a while loop. Notice that the condition on the while loop is current_node_next_node.next_node != self.head. We want to advance to the second to last element in this list.

Once we have positioned current_node to the proper index in the list, we remove the last node by pointing current_node.next_node at self.head. The removed Node ends up getting grabage collected by Python’s runtime.

### remove

The remove method supports removing items from the middle of the list. It has to account for the same cases as insert.

```def remove(self, position):
if position == 0:
# Case 1
self.remove_first()
elif position == self.size():
# Case 2
self.remove_last()
else:
if 0 < position < self.size():
# Case 3
current_pos = 0

while current_pos < position - 1:
current_node = current_node.next_node
current_pos += 1

current_node.next_node = current_node.next_node.next_node
else:
# Case 4
raise IndexError
```

Once again, we are going to dicuss case 3. We start with current_node pointing at self.head.next_node and current_pos = 0. We traverse the list until we arrive at the Node located before position. Now we nust point current_node.next_node at current_node.next_node.next_node. The removed Node gets garbage collected by the Python runtime.

### fetch

This method let’s us get data out of the list.

```def fetch(self, position):
if 0 <= position < self.size():
current_pos = 0

while current_pos < position:
current_node = current_node.next_node
current_pos += 1

return current_node.element
else:
raise IndexError
```

After checking position to make sure it's valid, we traverse the list until we arrive at the position. Then we return current_node.element. If position isn't valid, we raise an exception.

## Conclusion

This code shows an example a circular linked list, but it’s a simple implementation that we could optimize. This implementation always starts at self.head and traverse the list to a required position, but it could operate by tracking the most recently accessed Node and starting traversals from that point rather than always starting at the front of the list.

## Doubly Linked List—Python

We saw in Singly Linked List the benefits of using a linked list over an array. Linked lists are data structures that grow and expand as needed. Not only is it easy to add an element at the beginning or end of a linked list, but it is also easy to insert elements at arbitrary positions within the list. We can also easily remove elements from the linked list when they are no longer needed.

The doubly linked list is a varient of the singly linked list. The main complaint about a singly linked list is that it can only traverse the list in one direction starting at the head and working until it reaches the end of the list. Clients may not notice a performance hit when operating on small lists, but large lists will almost certainly have a performance hit.

Doubly linked lists work by tracking both the next node and previous nodes in the list. Tracking both nodes creates more overhead when inserting or removing from the list, but it allows the list to work in a bi-directional fashion. That can allow for a major performance boost when operating on large lists.

Here is the entire code followed by an explanation as to how it works. Note that the topic is the Doubly Linked List so I won’t be covering the unit testing code in this module, but I did include for those who are interested.

```class Node:
def __init__(self, element=None, next_node=None, prev_node=None):
self.element = element
self.next_node = next_node
self.prev_node = prev_node

def __str__(self):
if self.element:
return self.element.__str__()
else:
return 'Empty Node'

def __repr__(self):
return self.__str__()

def __init__(self):
self.tail = Node(element='Tail')

def size(self):
count = 0

while current is not None and current != self.tail:
count += 1
current = current.next_node

return count

def insert_front(self, data):

def insert_last(self, data):
node = Node(element=data, next_node=self.tail, prev_node=self.tail.prev_node)
self.tail.prev_node.next_node = node
self.tail.prev_node = node

def insert(self, data, position):
if position == 0:
self.insert_front(data)
elif position == self.size():
self.insert_last(data)
else:
if 0 < position < self.size():
count = 0
while count < (position - 1):
current_node = current_node.next_node
count += 1

node = Node(element=data, next_node=current_node.next_node, prev_node=current_node)
current_node.next_node.prev_node = node
current_node.next_node = node
else:
raise IndexError

def remove_first(self):

def remove_last(self):
self.tail = self.tail.prev_node
self.tail.next_node = None

def remove(self, position):
if position == 0:
self.remove_first()
elif position == self.size():
self.remove_last()
else:
if 0 < position < self.size():
current_pos = 0

while current_pos < position:
current_node = current_node.next_node
current_pos += 1

next_node = current_node.next_node
prev_node = current_node.prev_node

next_node.prev_node = prev_node
prev_node.next_node = next_node
else:
raise IndexError

def fetch(self, position):
if 0 <= position < self.size():
current_pos = 0

while current_pos < position:
current_node = current_node.next_node
current_pos += 1

return current_node.element
else:
raise IndexError

import unittest
from random import randint

names = ['Bob Belcher',
'Linda Belcher',
'Tina Belcher',
'Gene Belcher',
'Louise Belcher']

def test_init(self):
self.assertIsNotNone(dll.tail)
self.assertEqual(dll.size(), 0)

def test_insert_front(self):
for name in TestDoublyLinkedList.names:
dll.insert_front(name)

def test_insert_last(self):
for name in TestDoublyLinkedList.names:
dll.insert_last(name)

for i in range(len(TestDoublyLinkedList.names) - 1):

def test_insert(self):
for name in TestDoublyLinkedList.names:
dll.insert_last(name)

pos = randint(0, len(TestDoublyLinkedList.names) - 1)

dll.insert('Teddy', pos)
self.assertEqual(dll.fetch(pos), 'Teddy')

def test_remove_first(self):
for name in TestDoublyLinkedList.names:
dll.insert_last(name)

for i in range(dll.size(), 0, -1):
self.assertEqual(dll.size(), i)
dll.remove_first()

def test_remove_last(self):
for name in TestDoublyLinkedList.names:
dll.insert_last(name)

for i in range(dll.size(), 0, -1):
self.assertEqual(dll.size(), i)
dll.remove_last()

def test_remove(self):
for name in TestDoublyLinkedList.names:
dll.insert_last(name)

dll.remove(1)

self.assertEqual(dll.fetch(0), 'Bob Belcher')
self.assertEqual(dll.fetch(1), 'Tina Belcher')
self.assertEqual(dll.fetch(2), 'Gene Belcher')
self.assertEqual(dll.fetch(3), 'Louise Belcher')

if __name__ == '__main__':
unittest.main()
```

## Node

Like the singly linked list, the doubly linked list begins with a Node class that holds the data contained in the list and references to the previous and next nodes. Here is the code for the Node.

```class Node:
def __init__(self, element=None, next_node=None, prev_node=None):
self.element = element
self.next_node = next_node
self.prev_node = prev_node

def __str__(self):
if self.element:
return self.element.__str__()
else:
return 'Empty Node'

def __repr__(self):
return self.__str__()
```

Here is a break down of each methods in the Node class.

### __init__

The __init__ method creates the node. All three of its parameters are optional, but basically we are setting the data contained in the node, and building refrences to next_node and previous_node.

```def __init__(self, element=None, next_node=None, prev_node=None):
self.element = element
self.next_node = next_node
self.prev_node = prev_node
```

### __str__ and __repr__

I found that there was more work invloved with debugging doubly linked list, so in this version of the Node class, I chose to implement __str__ and __repr__ so that I could easily identify each Node in my debugger.

```def __str__(self):
# Check if self.element is null
if self.element:
# Just return the string representation
# of self.element
return self.element.__str__()
else:
# Otherwise return Empty Node
# to indicate the node does not
# hold data
return 'Empty Node'

# We are just going to reuse the
# code in __str__ here
def __repr__(self):
return self.__str__()
```

## Doubly Linked List

This is an example of a doubly linked list. It’s not opitmized for the simply fact that we are learning. If you need an optimized list structure, you Python’s list object.

```class DoublyLinkedList:
def __init__(self):
self.tail = Node(element='Tail')

def size(self):
count = 0

while current is not None and current != self.tail:
count += 1
current = current.next_node

return count

def insert_front(self, data):

def insert_last(self, data):
node = Node(element=data, next_node=self.tail, prev_node=self.tail.prev_node)
self.tail.prev_node.next_node = node
self.tail.prev_node = node

def insert(self, data, position):
if position == 0:
self.insert_front(data)
elif position == self.size():
self.insert_last(data)
else:
if 0 < position < self.size():
count = 0
while count < (position - 1):
current_node = current_node.next_node
count += 1

node = Node(element=data, next_node=current_node.next_node, prev_node=current_node)
current_node.next_node.prev_node = node
current_node.next_node = node
else:
raise IndexError

def remove_first(self):

def remove_last(self):
self.tail = self.tail.prev_node
self.tail.next_node = None

def remove(self, position):
if position == 0:
self.remove_first()
elif position == self.size():
self.remove_last()
else:
if 0 < position < self.size():
current_pos = 0

while current_pos < position:
current_node = current_node.next_node
current_pos += 1

next_node = current_node.next_node
prev_node = current_node.prev_node

next_node.prev_node = prev_node
prev_node.next_node = next_node
else:
raise IndexError

def fetch(self, position):
if 0 <= position < self.size():
current_pos = 0

while current_pos < position:
current_node = current_node.next_node
current_pos += 1

return current_node.element
else:
raise IndexError
```

### __init__

This doubly linked list implementation makes use of a head and a tail node. We traversing the list, the code will look for either head or tail as a means to detect if we are at the end of the list.

```def __init__(self):
self.tail = Node(element='Tail')

```

We pass the strings “Head” and “Tail” as the element data for each of these nodes for debugging purpose. Since Node implements __str__ and __repr__, we will see Heads and Tail in the debugger (at least when using PyCharm).

The next two lines do the work of pointing head and tail at each other. When the list is created, head.next_node is tail. Conversly, tail.prev_node is head. This indicates an empty list.

### size

This code inefficient on purpose. A better implementation would use a length varaible and increment or decrment it as Nodes are added and removed. However, in this case, I wanted to show how to traverse a list without the clutter of adding or removing Nodes.

```def size(self):
count = 0

while current is not None and current != self.tail:
count += 1
current = current.next_node

return count
```

We start by making a count variable and a current variable that points at self.head.next_node. It’s really important that we use self.head.next_node instead of self.head because self.head’s purpose is to mark the beginning of the list, not contain data. If we failed to make this distinction, the size of the list will be off by one.

Now, we are going to traverse the list increment count by one an each iteration of the loop. We should check for None for defensive programming purposes, but the critical part of the loop condition is if current != self.tail. The self.tail Node marks the end of the list and so we do not wish to include it in the size of our list.

### insert_front

This method inserts a new Node at the front of the Linked List.

```def insert_front(self, data):
```

Nodes inserted at the front of the list need their prev_node to point at self.head and their next_node to point at the Node located at self.head.next_node.

To complete the insertion, we now need to update self.head.next_node.prev_node to point back at the new Node that we created (otherwise, it would still point at self.head). Likewise, we need to update self.head.next_node to point at our new Node.

### insert_last

The code for this is almost identical to insert_head with the main difference being that we are working on the tail Node rather than the head Node.

```def insert_last(self, data):
node = Node(element=data, next_node=self.tail, prev_node=self.tail.prev_node)
self.tail.prev_node.next_node = node
self.tail.prev_node = node
```

Once again, we create a new Node. It’s next_node has to point at self.tail and it’s prev_node needs to point at self.tail.prev_node.

To complete the insertion, we need to update self.tail.prev_node.next_node to point at our new Node (otherwise it will continue to point at self.tail). We also need to point self.tail.prev_node at our new Node.

### insert

This method handles the insertion of a Node into the middle of the list. Keep in mind that it has to handle four possible cases.

• Position could be 0 (front of list)
• Position could be equal to size() (end of list)
• Position is in the middle of the list
• Position is size() (out of bounds)
```def insert(self, data, position):
if position == 0:
# First case, we insert at front
self.insert_front(data)
elif position == self.size():
# Second case, we insert at end
self.insert_last(data)
else:
if 0 < position < self.size():
# Third case, insert in middle
count = 0
while count < (position - 1):
current_node = current_node.next_node
count += 1

node = Node(element=data, next_node=current_node.next_node, prev_node=current_node)
current_node.next_node.prev_node = node
current_node.next_node = node
else:
# Fourth case, index out of bounds
raise IndexError
```

The comments point out how this code handles each of the possible cases. Let's focus on the insertion. We begin by creating a count variable and current_node variable. Now, we need to traverse the list until we arrive at the node right before the desired position. Each interation of the loop requires us to point current_node at current_node.next_node.

Once we arrive at our destination, we create a new Node. We will point it's next_node at current_node.next_node and its prev_node at current_node. Doing the insertion at this point in the list causes the new node to appear at the position index specified in the method parameters.

To complete the insertion, we point current_node.next_node.prev_node at the new Node (otherwise it would still point at current_node). Likewise, we point current_node.next_node at our new Node.

Again, I should mention that this code is inefficient again. Normally we would make use of the bidirectional capabilities of the list and decide if we want to traverse the list in forward (like we do here) or in reverse. For example, if we are trying insert into a list at size() – 2, it doesn't make sense to start at the head node and traverse forward to find our insertion point when we could start at tail and move backwards.

### remove_first

It’s really simple to remove nodes from the head of a doubly linked list.

```def remove_first(self):
```

You will notice that all we need to do is point self.head at self.head.next_node. Then we just set self.head.prev_node to None so that it doesn’t continue to point at the old self.head. The old Node will get garbage collected by the Python runtime.

### remove_last

Removing the tail from the list is just as easy as removing the head.

```def remove_last(self):
self.tail = self.tail.prev_node
self.tail.next_node = None
```

In this case, we just point self.tail at self.tail.prev_node. To remove the old Node, we next poitn self.tail.next_node to None. The old Node will get garbage collected by the Python runtime.

### remove

Remove needs to handle the same cases that insert has to handle. Otherwise it works by traversing the list of the position to remove and deletes the Node.

```def remove(self, position):
if position == 0:
# First case, remove at front
self.remove_first()
elif position == self.size():
# Second case, remove from end
self.remove_last()
else:
if 0 < position < self.size():
# 3rd case, remove at middle
current_pos = 0

while current_pos < position:
current_node = current_node.next_node
current_pos += 1

next_node = current_node.next_node
prev_node = current_node.prev_node

next_node.prev_node = prev_node
prev_node.next_node = next_node
else:
# 4th case, invalid position
raise IndexError
```

Again, we will focus on the removal part of this code. We begin by traversing the list to find our desired position. This time, we stop at the actual position rather than the position before it. Once we arrive at our desired position, we store current_node.next_node and current_node.prev_node into their respective variables. To remove current_node, we simply need to point next_node.prev_node at prev_node and likewise point prev_node.next_node to next_node. Since there are no longer any references to current_node, it is garbage collected by the Python runtime.

### fetch

Our final method is the fetch method, which let’s use retreive items from the Linked List.

```def fetch(self, position):
if 0 <= position < self.size():
current_pos = 0

while current_pos < position:
current_node = current_node.next_node
current_pos += 1

return current_node.element
else:
raise IndexError
```

By now readers should be familiar with how the list is traversed. We simply traverse the list to the desired position and return the Node.element to the caller.

## Conclusion

Doubly linked list are a more advance form of linked list that supports bi-directional traversals. Bi-directional navigation is possible because each Node in the linked list stores a refrence to both the next and previous Node in the list.

Our linked list implementation only utilizes forward direction traversal to help keep the implementation simple. However, it would only require a small amount of work to make use of reverse traversals.

## Singly Linked List—Python

Many of my programming students get asked to implement Linked Lists as a way to learn about data structures in programming. Now I am going to be very honest about this topic when it comes to Python. Python does not use fixed sized arrays list Java or C++ use. Instead, Python has a list object as a built in data type that grows and shrinks as needed.

For this reason, I can’t see any practical purpose to implementing a Linked List in Python other than for learning purposes (of course, that said, Java and C++ libraries have data structures that shrink and grow as needed also, so again, not sure why we would ever need to write our own linked list). That being said, I think there is value in learning about data structures such as Linked Lists.

## Arrays

Many programming languages have a data structure called an array. Arrays are a lot like an egg carton. There are x number of slots in which you can place an egg. Here is an example of an array in Java.

```String [] phoneNumbers = new String;
phoneNumbers = "867-5309";
phoneNumbers = "978-6410";
//and so on...
```

We use arrays for the same reason that we use lists in Python. They allow us to group common data together into a single variable. So we could iterate through this example array like this…

```for (String num : phoneNumbers){
System.out.println(num);
}
```

Arrays are extremely efficient in that you can easy create an array, fill it with data, and process that data. Nevertheless, Array’s have a major limitation. They are a fixed size. We are not able to grow or shrink and Array.

Linked Lists are a data structure that give us the convience that is offered by an Array, but also allows us to grow and shrink the data structure as needed. We even get the added bonus of being able to insert elements into random positions in the array.

Linked list work on the idea that inside of the Linked List we have a Node object that contains the data for that particular node and a reference to the next node in the list. By manipulating where the node points, we can expand or shrink the list as needed. We can also insert items into the middle of the list by pointing the node at different Node objects within the list.

The Linked List itself is a container class for the Nodes. It does the work of creating Nodes, removing Nodes, and updating Nodes. Before we get into complete detail, here is the code for a simple Singly Linked List written in Python.

```class Node:
def __init__(self, element=None, next_node=None):
self.element = element
self.next_node = next_node

def __init__(self):

def size(self):
count = 0

while current is not None:
count += 1
current = current.next_node

return count

def insert_front(self, data):
node = Node(element=data, next_node=self.head)

def insert_last(self, data):
else:
while current_node.next_node is not None:
current_node = current_node.next_node
current_node.next_node = Node(element=data)

def insert(self, data, position):
if self.head is None:
else:
if position > self.size() or position < 0:
raise IndexError
else:
if position == 0:
self.insert_front(data)
elif position == self.size():
self.insert_last(data)
else:
pos = 0
while pos < (position - 1):
temp = temp.next_node
pos += 1

next_node = temp.next_node
temp.next_node = Node(element=data, next_node=next_node)

def remove_first(self):
if self.head is not None:

def remove_last(self):
if self.head is not None:
prev = None

while current_node.next_node is not None:
prev = current_node
current_node = current_node.next_node

if prev is None:
else:
prev.next_node = None

def remove(self, position):
if self.head is not None and position == 0:
self.remove_first()
elif self.head is not None and position == self.size():
self.remove_last()
else:
if position  self.size():
raise IndexError
pos = 0
while pos < (position - 1):
pos += 1
current_node = next_node
next_node = current_node.next_node

current_node.next_node = next_node.next_node

def fetch(self, position):
if self.head is None:
return None
elif position  self.size():
raise IndexError
else:
pos = 0
while pos != position:
pos += 1
current_node = current_node.next_node

return current_node.element

import unittest
from random import randint

names = ['Bob Belcher',
'Linda Belcher',
'Tina Belcher',
'Gene Belcher',
'Louise Belcher']

def test_init(self):

def test_insert_front(self):
for name in TestSinglyLinkedList.names:
sll.insert_front(name)

def test_insert_last(self):
for name in TestSinglyLinkedList.names:
sll.insert_last(name)

for i in range(len(TestSinglyLinkedList.names) - 1):

def test_insert(self):
for name in TestSinglyLinkedList.names:
sll.insert_last(name)

pos = randint(0, len(TestSinglyLinkedList.names) - 1)

sll.insert('Teddy', pos)
self.assertEqual(sll.fetch(pos), 'Teddy')

def test_remove_first(self):
for name in TestSinglyLinkedList.names:
sll.insert_last(name)

for i in range(sll.size(), 0, -1):
self.assertEqual(sll.size(), i)
sll.remove_first()

def test_remove_last(self):
for name in TestSinglyLinkedList.names:
sll.insert_last(name)

for i in range(sll.size(), 0, -1):
self.assertEqual(sll.size(), i)
sll.remove_last()

def test_remove(self):
for name in TestSinglyLinkedList.names:
sll.insert_last(name)

sll.remove(1)

self.assertEqual(sll.fetch(0), 'Bob Belcher')
self.assertEqual(sll.fetch(1), 'Tina Belcher')
self.assertEqual(sll.fetch(2), 'Gene Belcher')
self.assertEqual(sll.fetch(3), 'Louise Belcher')

if __name__ == '__main__':
unittest.main()
```

## Implementation Details

As you can see, we are using Python’s OOP features to implement this linked list. That being said, this could have been down procedurally also. There is nothing that says linked lists have to be implemented in terms of classes and objects.

### Node

The Node is the most basic element in the linked list. It has the responsibility to contain the data and point to the next node. That’s all it does. Here is the code for our Node class

```class Node:
def __init__(self, element=None, next_node=None):
self.element = element
self.next_node = next_node
```

The SinglyLinkedList class is the actualy linked list implementation. This is called SinglyLinkedList because each of the Nodes only link in a single direction (as opposed to a doubly linked list, which is bi-directional).

Here is the code for SinglyLinkedList followed by an explanation of each method.

```class SinglyLinkedList:
def __init__(self):

def size(self):
count = 0

while current is not None:
count += 1
current = current.next_node

return count

def insert_front(self, data):
node = Node(element=data, next_node=self.head)

def insert_last(self, data):
else:
while current_node.next_node is not None:
current_node = current_node.next_node
current_node.next_node = Node(element=data)

def insert(self, data, position):
if self.head is None:
else:
if position > self.size() or position < 0:
raise IndexError
else:
if position == 0:
self.insert_front(data)
elif position == self.size():
self.insert_last(data)
else:
pos = 0
while pos < (position - 1):
temp = temp.next_node
pos += 1

next_node = temp.next_node
temp.next_node = Node(element=data, next_node=next_node)

def remove_first(self):
if self.head is not None:

def remove_last(self):
if self.head is not None:
prev = None

while current_node.next_node is not None:
prev = current_node
current_node = current_node.next_node

if prev is None:
else:
prev.next_node = None

def remove(self, position):
if self.head is not None and position == 0:
self.remove_first()
elif self.head is not None and position == self.size():
self.remove_last()
else:
if position  self.size():
raise IndexError
pos = 0
while pos < (position - 1):
pos += 1
current_node = next_node
next_node = current_node.next_node

current_node.next_node = next_node.next_node

def fetch(self, position):
if self.head is None:
return None
elif position  self.size():
raise IndexError
else:
pos = 0
while pos != position:
pos += 1
current_node = current_node.next_node

return current_node.element
```

#### __init__

The __init__ method in SinglyLinkedList is small but important. This method creates the self.head variable, which is the first Node in the list. You will notice that we set it to None to indicate an empty list.

```def __init__(self):
```

#### size

The size method is used to calculate the size of the linked list. It works by traversing every single node in the list and keeping a count of each node it passes.

```def size(self):
count = 0

while current is not None:
count += 1
# Advance to the next node by setting
# current to the next node
current = current.next_node

return count
```

This is a good method to help us understand the mechanics of the linked list. You will notice how each node has a next_node object attached to it. That next_node is either a Node or it is None. None is used to indicate that we have reached the end of the list.

We begin by creating a varaible `current` and referring it to `self.head`. Next we start a loop that terminates when `current` is None. Inside of the loop, we increment count by one, then advance to the next node. When `current` is None, we are at the end of the list and we can return the count as the size of the list.

#### insert_front

This method let’s us quickly add an item to the beginning of the linked list.

```def insert_front(self, data):
node = Node(element=data, next_node=self.head)
```

We create a new node and set it’s next_node to self.head. Then we just update self.head to refer to the new node. Since the new node is now the head of the list, we have added the item to the front of the list.

#### insert_last

This method let’s us add an item to the end of the list really quickly.

```def insert_last(self, data):
# If this is the first insertion,
# then we can just make it the head
else:
# We need to advance to the
# end of the list
while current_node.next_node is not None:
current_node = current_node.next_node

# Now we can grow the list by adding
# a new node and pointing current_node.next_node at it
current_node.next_node = Node(element=data)
```

The first thing to do is check if the list if empty. We know it’s empty if self.head is None. If that’s the case, then just make self.head point at a brand new node.

If the list isn’t empty, then we need to advance to the end of the list and insert the new new node there. It’s not hard to do this. We start by creating a current_node variable and referring it to self.head. Then we just loop until current_node.next_node is None. In each iteration of the loop, we point current_node at current_node.next_node.

Finally we just set current_node.next_node to a new Node object. At this point, current_node referring to the last node in our list, so pointing it’s next_node at the new node is what adds to the list.

#### insert

The insert method is a little more complicated than the other two inserts. In this case, we are trying to insert a new Node into the middle of the list at an arbitrary position. The mechanics of this isn’t that difficult. What we need to do is find the node at the specified position and store it’s next_node in a variable. Then we create a new Node and set current_node.next_node to the new Node. The new Node’s next_node becomes the old curent_node.next_node.

However, we have a couple of conditions to check for first. For one thing, the list may be empty. The client may try to insert an item at a negative position or a position that is beyond the list’s size. They may also pass in a position that is 0, which means they are inserting in the front of the list. They could also specify a position that is equal to size, which means they could be trying to add to the end of the list. Therefore, our insert method needs to consider all of these cases.

```def insert(self, data, position):
if self.head is None:
# This is the empty list case
# Once again, we can just insert at head
else:
if position > self.size() or position < 0:
# This is the case where they are
# trying to insert at a negative index
# or beyond the size of the list.
# Just raise an exception since that's a
# programming error on the client's part
raise IndexError
else:
if position == 0:
# If they are trying to insert at 0, we
# can use self.insert_front to do the work
# for us.
self.insert_front(data)
elif position == self.size():
# Likewise, we can just use
# self.insert_last to handle cases
# where position is the size() of the list
self.insert_last(data)
else:
# to hold the current node
pos = 0 # track the position

# We actually want to stop at right before
# the position so that the new node is
# inserted at the specified position
while pos < (position - 1):
# Advance to the next node
temp = temp.next_node
# Update position
pos += 1

# Store the next node into a tempory variable
next_node = temp.next_node

# Now set temp.next_node to a new Node object (and set the
# now node's next_node to the old next_node)
temp.next_node = Node(element=data, next_node=next_node)
```

You'll want to read through the comments of this code to get a better understanding of what is happening. Most of the code here we have seen already and we are just reusing code when possible. The important part to understand is when we do the insertion. We need to traverse the linked list to one node before where we want to insert the node.

Now keep in mind, that the current node, temp, has a next_node variable. We need to store that so that we don't lose the last half of the list. So we store it in a next_node variable. Now to actually perform the insertion, we point temp.next_node to a new Node object. Since the Node's __init__ method can take an option next_node argument, we just pass the next_node that we saved to the __init__ method. Thus, we insert a new node at the expected position.

#### remove_first

When we want to remove the first element in a linked list, we just need to set head to the next node in the list.

```def remove_first(self):
if self.head is not None:
```

The only real thing we need to watch out for is if the list is empty. As long as the list isn’t empty, we just set self.head to self.head.next_node and the first item will get garbage collected by the Python runtime.

#### remove_last

In the case of removing the last item from the list, we can just traverse to the end of the linked list and set the 2nd to last Node’s next_node to None.

```def remove_last(self):
if self.head is not None:
# Start at the head
prev = None

# Loop
while current_node.next_node is not None:
prev = current_node
current_node = current_node.next_node

if prev is None:
else:
prev.next_node = None
```

Once again, we need to make sure the list isn’t empty. Then we go through the list until we get to the end of the list. While we traverse the list, we keep a reference to the node that is before current_node. It is this node’s next_node that we are setting to None, rather than current_node.next_node. The result will be that current_node is disconnected from the linked list and garbage collected.

We do have to account for the possibility that we are removing all items in the linked list. If prev is none, then we need to set self.head to None, which results in an empty linked list.

#### remove

This method let’s us remove a node at a specified position. It has the same special cases as it’s insert counter part so we aren’t going to cover them here. The idea is that we are taking a Node out of the list by taking the previous nodes next_node and pointing it at the deleted node’s next_node. The deleted Node gets garbage collected by the runtime.

```def remove(self, position):
if self.head is not None and position == 0:
# Just use remove_first() remove the first item
self.remove_first()
elif self.head is not None and position == self.size():
# or remove_last() to get rid of the last item
self.remove_last()
else:
# Throw an exception if we are out of bounds
if position  self.size():
raise IndexError
# Track the current position
pos = 0

# Track the current and next_nodes

# Traverse through the list but stop at the node
# right before the one we are deleting
while pos < (position - 1):
pos += 1
current_node = next_node
next_node = current_node.next_node

# Now, make current_node.next_node point to
# next_node.next_node. This removes next_node
current_node.next_node = next_node.next_node
```

Once again, the comments will be helpful here. The idea is that we want to remove the node that is in between current_node and next_node.next_node. We can do that very easily by pointing current_node.next_node to next_node.next_node. The next_node object in between the two is severed from the list and garbage collected.

#### fetch

Our final method is used to retreive items from the list. By now, you have seen plenty of examples on how to traverse the list. In this case, we just traverse the list to the specified position and then return Node.element

```def fetch(self, position):
if self.head is None:
return None
elif position  self.size():
raise IndexError
else:
pos = 0
while pos != position:
pos += 1
current_node = current_node.next_node

return current_node.element
```

Of course, we need to check for empty lists and perform bounds checking. Otherwise, it’s simple enough to look up the value at the specified position.

## Unit Testing

We aren’t going to cover unit testing in detail here, but I did provide an example class that tests this linked list implementation. Not only does it provide an example of Python’s unit testing framework, but it also shows how this class could be used. However, don’t use this class in production code. Python’s list object is a much better choice.

## Conclusion

Linked lists are a way to create dynamically expanding collections of data. This tutorial demonstrated how to create a singly linked list. The key to understanding linked lists is to understand how the lists makes use of it’s Nodes. Once you have a solid understanding of how the nodes work, it’s relatively straight forward to make a linked list.