Day 34: Rough Attempt – Singlely Linked Lists

Started on my Journey to write out my own Linked list data structures. It’s pretty intimidating for some reason. Thankfully with all the amazing resources available online I can literally start by just copying someone else. We will live in an amazing era for learning! I watched a few videos on the topic and I’m confident I understand it but I only got to implementing a very hard coded version of a singely linked list in C. Next up is to try and refine (Or at least copy out someone’s more refined code LOL).

TLDR;

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

  • Started on the “Technical Questions” section of the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. I decided to get a quick intro to the C and Java programming languages before I jump into building the core data structures and algorithms that will be covered in most all technical interviews. Started working on Singely Linked lists. Copied out a rough version of a linked list that cannot be modified or added to but holds the required structure.

Notes from Linked List Attempt

Code

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


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

// Allows us to write out node_t instead of struct node everytime we use that type.
typedef struct node node_t;

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

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

int main(){

  node_t n1, n2, n3;
  node_t *head;

  n1.value = 50;
  n2.value = 30;
  n3.value = 25;

  //Link them up
  head = &n3;
  n3.next = &n2;
  n2.next = &n1;
  n1.next = NULL;  //So that we know when to stop. (end of the list)

  printList(head);

  return 0;
}

Pointers are a type of data that works with the memory address of variables. Just like with any other data type we can store them in a variable. The difference is that pointers reference existing variables. We cannot make up an address in memory (I think…).

The syntax is also weird so here are some examples:

int age = 30;
int *pAge = &age;

double gpa = 3.4;
double* pGpa = &gpa;

char grade = 'A';
char * pGrade = &grade;

We will often see them without the space between the “*” and the pointer variable name. For example: char pAge = &age. Ultimately there is no difference in functionality but the *pVar syntax with no spaces is preferred most of the time. Here is a great question/answer on the subject from Stack Overflow.


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.