Day 42: Pointer -> Pointer -> Pointer -> etc

Made a lot of slow progress with pointers to pointers. I am starting to accurately conceptualize the values of pointers and the values of the dereferenced pointers. I have so much more work to go before I get comfortable but at least it’s a step in the right direction. Once I understood pointers to pointers, I was able to make the next improvement to my linked list’s insert_at_head function.

TLDR;

Okay, so here are the highlights of what I did:

  • Continued work on the “Technical Questions” section of the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. Continued my study of Linked Lists. I am trying to improve on my first example while noting what I am changing and how the improvements help improve the quality of my code. I successfully made the changes to the insert_at_head function. I think I understand what is going. There is soo much pointing it has me confused for sure LOL.

Improvement Number 4:

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

struct node {
  int value;
  struct node* next;
};

typedef struct node node_t;

node_t *create_new_node(int value){
  node_t *result = malloc(sizeof(node_t));
  result->value = value;
  result->next = NULL;
  return result;
}

// Add function to insert a new node as the new head of the list
// Change the head argument to be a pointer to a pointer so that
// we change the head pointer in the function instead of in the
// for loop of the main function.
node_t *insert_at_head(node_t **head, node_t *node_to_insert){
  // set the next pointer of the new node to the same value as the
  // current head pointer. Once the new node is linked, the the head
  // pointer can be changed to point to the newly inserted node.
  // instead of the previous one.
  node_to_insert->next = *head;
  *head = node_to_insert;
  return node_to_insert;
}

void printList(node_t *head){
  node_t *temporary = head;

  while(temporary != NULL){
    printf("%d - ", temporary->value);
    temporary = temporary->next;
  }
  printf("\n");
}

int main(){

  node_t *head = NULL;
  node_t *tmp = NULL;

  for(int i = 0; i < 10; ++i){
    tmp = create_new_node(i);
    // Replace the head assignment to take place with the insert
    // function.
    //head = insert_at_head(head, tmp);
    insert_at_head(&head, tmp);
  }

  printList(head);

  return 0;
}

The output is the same as the previous programs:

9 - 8 - 7 - 6 - 5 - 4 - 3 - 2 - 1 - 0 -

Conclusion

That’s all for today. This is my sixth round of the “#100daysofcode” challenge. I will be continuing my work from round five into round six. I am currently working through the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. My goal is to become more familiar with algorithms and data structures. This goal was derived from my goal to better understand operating systems and key programs that I use in the terminal regularly e.g. Git. This goal was in term derived from my desire to better understand the fundamental tools used for coding outside of popular GUIs. This in turn was derived from my desire to be a better back-end developer.

I have no idea if my path is correct but I am walking down this road anyways. Worst case scenario I learn a whole bunch of stuff that will help me out on my own personal projects.