Featured image of post The Unordered List (Singly Linked List) Implementation

The Unordered List (Singly Linked List) Implementation

A simple guide on singly linked list (Unordered list)

Understanding Unordered List

The simplest way to describe about unordered list is it is a container of items with no ordering, meaning it can be anywhere on the container. This list contains a collection of individual nodes that has pointer to another node. It is important to know about Node before learning unordered list.

Node: A node is a container which has two fields [data, next]. Data contains the actual value on the node and next contains the pointer to the next node if there is any otherwise it contains null.

To understand it more clearly here is the figure that explains the entire analogy of Singly unordered list aka. Singly Linked List. Image Description

We have list of items that starts with 5 also known as head of the list, which then points to the other data of the list.

Things to point

  1. Each item on the list is a node not an individual data, where node contains both data and the pointer to another node known as next.
  2. The only reference of the list is set by the head which points to the latest node inserted.
  3. The list which is created only knows the head of the whole linked list so there is no way to get the value by index.

How to add a node to the list?

There are many ways to add data to the list, we can add either way , to the front or to the end of the list. The order of the list is not important as this is unordered. Here i will show you how to add to the front of the list i.e the head of the list. Since we already know the head of the list as the only reference to the list is the head. We do the following to add the item to the list.

  1. Create a node with data, and the pointer to null. Image Description
  2. We need to store this on some variable because we need to point it’s next to the current head of the list.Image Description
  3. Then we point the next of the temp to the current head and then assign the head to the temp.Image Description

Python code to implement the list

1
2
3
4
def add(self, data):
        temp = Node(data)
        temp.set_next(self.head)
        self.head = temp
Time Complexity: Since we only have to work with the first pointer the time complexity is O(1).

Getting the size of the List

How do we get the size of the list? Can you try and think of a solution to get the size? The most obvious thing that comes to mind is going through all the nodes and counting them right? YES! that’s the solution. How do we implement that? 1. First thing is we need something to store the count of the list. 2. Then we start from the head (because we only have head as the reference of the list) 3. We then go step by step and increase the count of the variable until we hit the null pointer of the Node. 4. When we hit the null we stop and return the count of the list.

Python code to implement the list

1
2
3
4
5
6
7
def size(self):
        count = 0
        current = self.head 
        while current != None:
            count += 1 
            current = current.get_next()
        return count
comments powered by Disqus