Day 41: A Pointer to a Pointer… Bruh Stop!

Continued my slow progress through linked lists. I made another improvement to the original hard coded linked list. I copied the tutorial I’m following and added a function that inserts a new node at the head of the linked list. This helped to abstract away some of the processes being done in the main function. This made the code cleaner. The challenge started when the tutorial suggested making our code even cleaner by using a “pointer to a pointer” so that we did not need to manually assign the pointer to the head node in for loop but could instead have that also be done with the insert_at_head() function. Long story short things just got messy.

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 added a insert_at_head() function that helped to abstract the work being performed into clearer processes. Started learning about “Pointer to Pointers” since it is being used in the next improvement shown in the tutorial.

Improvement Number 3:

#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
node_t *insert_at_head(node_t **head, node_t *node_to_insert){
  node_to_insert->next = head;
  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 previous two statements with function that performs
    // the same actions but abstracts the actions in a functional
    // programming style.
    //tmp->next = head;
    //head = tmp;
    head = 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.