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,
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!
#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;
}
Node *search(int item) {
if (start == NULL) {
cout << "List is empty!\n";
return NULL;
}
Node *temp = start;
while (temp != NULL) {
if (temp->data == item) {
return temp;
}
temp = temp->link;
}
return NULL;
}
};
int main() {
LinkedList list;
list.search(5);
}
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.
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,
void insertIntoLocation(Node *location, int item) {
if (location == NULL) {
insertFirst(item);
return;
}
Node *new_node = new Node(item);
new_node->link = location->link;
location->link = new_node;
}
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.
It has a limitation. While inserting into a sorted list, if the item is greater than all the elements in the list, it will not insert the item at the end of the list. It will insert the item at the beginning of the list. So, we need to modify the code to handle this case.
Use 25 instead of 15 in the above code, you will get the fact.
To solve this issue, we can use an another method like,
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.
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.
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,