Adding elements in the middle of a list

Well, if you managed till here I must really applaud your patience. This section might be a bit tough, but I will try and keep it as simple as I can.

As usual we start off by looking at some code. This time the program actually does some useful work it is an insertion sort program. The algorithm for insertion sort is really very simple. Our program receives numbers one after another. We compare this number with the numbers we have in a list and insert it in an appropriate position. Though I must say this is not a very good sorting algorithm, performance wise. The code can be downloaded here.

Phew! That was some bit of code. But never fear when Reuben is near. We will take that apart part by part. If you do notice this program has made use of both the concept's I've explained till now. That is inserting elements in the beginning and inserting elements at the end.

The main function

This is simple all we do here is create a pointer to hold the list, then create an array to be sorted. Line 3 calls the sort function, and ofcourse you should know what Line 4 does by now.

The InsertionSort function

The work of this function is simple it just goes through the array calling the insert function which inserts the element in the right position.

The Insert function

Now we have the juicy part. This is the function where all the action happens. Let us start off by looking at the variables used. At line 0 as usual we create a new node for the list and in line 4 and 5 the assignment of the same happens. In line 1 we have the temp variable which is used to traverse the list. In line 2 we have the temp1 variable that is used to traverse the list once we have traversed the list. I'll explain that statement in a while.

The section of code above checks to see weather the element inserted is the first one or not. If it is the first one then the first pointer is made to point to it and the first pointer is returned. If we escape the above "if" construct unscathed we land in to this weird looking for loop. This is a more compact way of traversing the list. Since we already assigned temp to first in the begining of the function we don't need to do it again. The condition for the loop is that temp must not become NULL, and next part of the loop is the actual traversal.

In the above code segment's if construct we check whether the number stored in the node greater than the number entered. If that is the case the we use the temp1 variable to traverse the list and stop in the node before temp. Thats what I meant when I said temp1 is used traverse the list once we have traversed the list. Pictorially it can be shown as follows. SEP

usage of temp1

This section of code gets control when the while loop breaks. If it happens that the temp points to the first node. The new node is inserted at the begining of this list.

The code above should be obvious. If temp is pointing to NULL that means that we are at the end of the list. Therefore, we insert the new node at the end of the list. This is the intresting part. Here we insert the node in the middle of the list. We make new_node point to temp. Once this is done we make temp1's next pointer point to new_node. This is shown in the diagrams below.SEP
in the middle of the list
in the middle of the list

I think that all there is to the program once you have mastered this section and the previous section you should be able to write simple programs that make use of link lists. In the next few sections I will be concentrating on removing elements from a link list.