By now we’ve covered enough of the basics to begin studying algorithms. For the earlier ones, we’ll hold your hand in the implementation details, but we strongly recommend that you implement all the algorithms and data structures yourself at least once without looking at sample code.

For now let’s look at a way to store an indefinite amount of data. In many programming languages, an object that can store an indefinite amount of items is called a list. So how do we make a list?

The first idea we can have would be to initially allocate an array for, say, ten items. When the list gets full and another item needs to be added, we can allocate a new array of size eleven, then copy everything over and free the original array. Doing this, every addition, we can just allocate a new array, copy everything over and delete the old one. This works, but it’s slow. Can we do better?

Of course, the next intuitive idea is to allocate more space when you need to resize your list. But really, you’re just prolonging the problem.

The next idea is this: what if we just allocate space for each item and store the items there? Well, in this case we’ll have to keep track of where the items are too, otherwise we’ll just have memory leaks and lose track of where our data is. Clearly, if we store an item together with its location, they’ll both be lost at the same time, so we have to store the location somewhere we can already find. This leads us to the most basic idea of the linked list. And at this point, a picture of what we mean will do much more than words.

In this picture, each big rectangle stores the data we want to store and a pointer to the next big rectangle. The last big rectangle’s pointer is the null pointer, or as previously discussed, a pointer to memory address 0. This marks that nothing follows after the fourth big rectangle. In computer language texts, you will find these big rectangles called nodes, and we will use that terminology here too.

To restate the obvious, a node contains some data and a pointer to the next node. This way, we only need to store a pointer to the first node, also called the head of the list, and we can find all the other nodes by following the pointers. The code for a node would look like this:

struct node {
int data;
node* next;
}


data can change. It could be an int or a char. You can name it whatever you want, and you can put as many members as you need there. The only important thing is that you keep a pointer to the next node.

So what would our operations on this look like? Let’s look at two things for now: Adding an item to the list and printing out the entire list.

#include <stdio.h>

struct node {
int data;
node* next;

node(int data) {
this->data = data;
this->next = 0;
}

// Look for the last node and store it in current
node* current = this;
while(current->next != 0) {
current = current->next;
}

// Create a new node
node* new_node = new node(x);

// Point the last node to the new node
current->next = new_node;
}

void print() {
node* current = this;
// Loop through each node until you hit the null pointer
while(current != 0) {
printf("%d\n", current->data);
current = current->next;
}
}
};

main() {
}


The above code works, but it requires us to initialize the one-element node first. Plus it’s weird to think about because it looks like we are adding data to an object of type node. If we write our code better, we can make it more pleasant to think about.

#include <stdio.h>

struct node {
int data;
node* next;

node(int data) {
this->data = data;
this->next = 0;
}
};

};

node* new_node = new node(x);
} else {
while(current->next != 0) {
current = current->next;
}

node* new_node = new node(x);
current->next = new_node;
}
};

void print() {
while(current != 0) {
printf("%d\n", current->data);
current = current->next;
}
}
};

main() {
list.print();
}


Isn't that code a little bit nicer to look at now that we’ve defined a linked_list object?

While we’re at it, we should also take care of not causing any memory leaks. Just add the following destructor to our linked list.

~linked_list() {
while(current != 0) {
// You will want to get a pointer to next before you free this one
// Otherwise, you'll get a segfault when you try to access memory after you freed it. :)
node* next = current->next;
delete current;
current = next;
}
};


The improved code quality also means we can easily add items to the start of the list:

void add_start(int x) {
// Create a new node and point it to the current start of the list
node* new_node = new node(x);

// Update the start of the list to be the newly-created node
};


If we had kept a reference to the head as we did before, add_start will end up a little bit messier because we would have to remember to update the local variables.

Also, we still have one very big problem with this: our linked list still doesn't solve our original problem of being able to add items quickly to the end! We still have to iterate through our entire list before we can add an item.

The modification we have to do isn’t too difficult. So before you move on, please think of a way to speed up insertion at the end.

You need to add a new member to linked_list to avoid slow computations.

You need to add a new member to linked_list, which points to the last node. Make sure to update it whenever you add a new node. The normal term used for this pointer is tail.

Once you are done, please implement the solution you have found.

### Removing Items

Now that we can add items, how do we remove items?

Removing items from the start is easy, just update the head then delete the previous head.

What about removing from the end? From the previous exercise, we’ve already figured out a fast way to get the last node. But if we delete that, how do we update our last node? And how do we make sure the new last node no longer points to a freed memory address?

To remove from the end, all you need to do is make sure each node also has a pointer to the node behind it. This is called a doubly linked list. The first one that we were discussing with only forward links is called a singly linked list.

Please implement a doubly linked list with removal from both sides. :D