C - Linked lists - Head and End

I hope you liked my fantastic simple example of linked list (OK it was not so exciting, but it works fine).

SO, let's try today a tutorial of a linked list where we will able to add and display elements in our linked list, first by adding elements from the head and then from the queue (or the end).

Let's start our example of this famous linked list by adding elements from the head and from the end:

#include        <string.h>
#include        <stdio.h>
#include        <stdlib.h>

typedef struct s_ll
{
  int   nb;
  struct s_ll *next;
} t_ll;

t_ll    *addHead(t_ll *linkedList, int i)
{
  t_ll  *new;

  new = malloc(sizeof(*new));
  new->nb = i;
  new->next = linkedList;
  return (new);
}

t_ll    *addEnd(t_ll *linkedList, int i)
{
  t_ll  *new;
  t_ll  *tmp;

  new = malloc(sizeof(*new));
  new->nb = i;
  new->next = NULL;
  if (linkedList == NULL)
    return (new);
  else
    {
      tmp = linkedList;
      while (tmp->next != NULL)
        tmp = tmp->next;
      tmp->next = new;
      return (linkedList);
    }
}

void    display(t_ll *linkedList)
{
  while (linkedList->next != NULL)
    {
      printf("linkedList->nb = %d\n", linkedList->nb);
      if (linkedList->nb == 1)
        linkedList = linkedList->next;
      linkedList = linkedList->next;
    }
  if (linkedList->next == NULL)
    printf("linkedList->nb = %d\n", linkedList->nb);
}

void    init(int av)
{
  t_ll  *linkedList;
  int   i;

  i = 0;
  linkedList = NULL;
  while (i <= av)
    {
      linkedList = addHead(linkedList, i);
      linkedList = addEnd(linkedList, i);
      i++;;
    }
  display(linkedList);
}

int     main(int ac, char *av[])
{
  if (ac < 2)
    {
      printf("Need an argument please. For example: 10. Try again!\n");
      exit(1);
    }
  init(atoi(av[1]));
  return (0);
}

Let's compile it and execute it in one single line:

$ gcc main.c && ./a.out 10

Let's see the result:

linkedList->nb = 10
linkedList->nb = 9
linkedList->nb = 8
linkedList->nb = 7
linkedList->nb = 6
linkedList->nb = 5
linkedList->nb = 4
linkedList->nb = 3
linkedList->nb = 2
linkedList->nb = 1
linkedList->nb = 0
linkedList->nb = 1
linkedList->nb = 3
linkedList->nb = 4
linkedList->nb = 5
linkedList->nb = 6
linkedList->nb = 7
linkedList->nb = 8
linkedList->nb = 9
linkedList->nb = 10

There you say: "OUAH that's so incredible! I love you man!".

OK, thank you I know I rox.

Let's explain what is happened.

The addHead() function is like the simple example. So nothing new here.

Let's see the addEnd() function now. We can see that I test if the linkedList value is NULL. If yes, I return the new structure.
If no, I create a temporary variable named tmp to save the linkedList variable.
And I say while tmp->next adress is not NULL, tmp is equal to the tmp->next address.
At the end of loop, we say that the tmp->next address is one of new.
We remplace the NULL value by the new's structure address.
As we already assign the NULL value to the new->next address, all is now OK.

We can display elements of the linked list that we added by the head and by the end, this at the same time as we can see in the init() function.

In the display() function we say that if the address of linkedList-> is equal to NULL, we display once the number stocked in the linkedList->nb variable.

Of course we add a condition in this display() function. We said that if the linkedList->nb is equal to 1, so we put the address of linkedList to one of linkedList->next.
We made it to not display twice the 0 value in the display() function.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.