Linked List

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers.

This page will be a bit large. Use the right sidebar to navigate withing the page (desktop mode).

The methodology

There are different ways to implement linked list, like using structures. But I will be using class (OOP) for simplicity. Feel free to use any other websites for exploring different approaches.

5.1 Traversing

To traverse we will use a simple loop. Check this headless code,

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* link;
    Node(int data) {
        this->data = data;
        link = NULL;
    }
};

class LinkedList {
private:
    Node *start;
public:
    LinkedList() {
        start = NULL;
    }
    void printAll() {
        if (start == NULL) {
            cout << "List is empty!\n";
            return;
        }
        Node *temp = start;
        while (temp != NULL) {
            cout << temp->data << " ";
            temp = temp->link;
        }
        cout << endl;
    }
};

int main() {
    LinkedList list;
    list.printAll();
}

Here for storing data we have used an structure. You may use a class as well for this. Here as for the constructor, we are assigning our head/ start node to NULL. In the printAll method we are using our actual algorithm. As the list is empty, it will print List is empty.

5.2 Searching

Just like in searching, we will traverse and compare!

Here, it'll also print the list is empty. So we actually need some method to append some data into our list so that we can actually display some output. Right?

Let's try altogether in the next example!

5.3 Searching a sorted list

The list is sorted? Yeah! So we can just terminate if we find a value higher that we are aiming.

5.4 Insert at the beginning

Well, here's two thing to consider, if a list is empty then you are the king, MR. DATA. And otherwise we just have to tweak our start/ header node a bit, right? Check the above code's insertFirst method.

5.5 Insert at a location

We can actually insert at a location if we know the location, right? Hope you can imagine that,

Here we are inserting an item (int) in the location. But the location is a pointer of our node, right? So how can we get this? We have already seen that in 5.2.

Let's assume it's a sorted list. Now what?

5.6 Location of Upper Bound - 1

What is upper bound? If I have an array with 1 2 2 4 elements, then the upper bound of 3 is position 3 (0 base index). Right?

But for our previous 5.4 algorithm to work, we need the location of last occurrence of 2, so our title is, upper bound - 1

Let's look at the code, here we need to trace the history.

5.7 Insertion in a sorted list

So if we implement two methods we developed in 5.4 and 5.5, our code will be,

5.8 Delete at a position

For deletion, we will require two locations,

  • location which value will be erased

  • location before the erased value

Why? If we don't have the previous value we need to traverse once more! Or perhaps 2 way linked list? We will check both of 'em. But wait a bit longer for 2 way linked list.

So our basic method is like,

5.9 Location and it's previous location!

We can use an extra variable for preserving previous location, right? Let's check a method,

5.10 Deletion at the first occurrence

Now combining 5.8 and 5.9 we can achieve this, right? Here's full code,

I think, this concludes the basics! There are even more things that can be achieved. So good luck! For now let's explore two more perspective,

  • header linked list

  • 2 way linked list

and finally we can combine them together into,

  • 2 way header linked list


Header linked lists

Let's think in this way, in linked lists, when we iterate, what is our sentinel (the point where we stop our loop) value? Well, it's until we actually find NULL. Right?

To make this looping steps a lil bit easy and NULL free, we are trying this header linked list! Out of two kinds, grounded and circular, we will be using circular.

5.11 Traversing Circular Header List

So just like our 5.1 Traversing, we are traversing, but a bit difference in the looping steps. Compare to understand.

5.12 Searching in Header list

We will return the first occurance (position). So our code is,

5.13 Searching location and previous location

Just like 5.9, let's implement this one,

5.14 Deletion in Header list

With the support of 5.13 we can finally delete a node just like 5.10, right?

Two Way Lists

In two way lists, besides storing the next elements location of each node, we will also gather the previous value's location. And it's more fun if we combine, two way lists and headers lists together! Let's rip it,

5.15 Deletion in 2-way header

So when we have the location, we can actually link the previous one and the next one altogether to perform teh deletion! So our method is like,

5.16 Insertion in 2-way header

Likewise, if we have the positions of two nodes, we can insert easily like,

Finally if we wrap 5.15 and 5.16 altogether,

Here I didn't show the implementation of every single items. So best wishes for you! It's more fun to implement rather than just reading. Good luck!


Python implementation

Once I implemented it on python. If you are familliar with python you can check that out for reference from time to time,

python implementation

Last updated

Was this helpful?