Day 43: Adding a find Function For Linked List

Added a function that finds a value in my linked list. It was pretty straightforward but once again pointers are killing my brain. I have to repeatedly walk through the concept of not changing variable values but rather changing where we are pointing a variable towards. I still need more practice for this.

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 the quality of my code. I made the return value void from the insert_at_head function and added a find_node function.

Improvement Number 5

#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;
}

// We currently don't need to return anything so let's make this void.
void insert_at_head(node_t **head, node_t *node_to_insert){
  node_to_insert->next = *head;
  *head = 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);
    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 -

Improvement Number 6

#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;
}

void insert_at_head(node_t **head, node_t *node_to_insert){
  node_to_insert->next = *head;
  *head = node_to_insert;
}

// Add a function that loop through list starting from the head 
// to try and find a node with a specific value.
node_t *find_node(node_t *head, int value){
  node_t *tmp = head;
  while(tmp != NULL){
    if(tmp->value == value) return tmp;
    tmp = tmp->next;
  }
  return NULL;
}

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);
    insert_at_head(&head, tmp);
  }

  // try to find number 5
  tmp = find_node(head, 5);
  printf("Found node with value: %i\n\n", tmp->value);

  printList(head);

  return 0;
}

The output is the same as the previous programs:

Found node with value: 5

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.