Completed the Implementation of a Singlely Linked List in C
. Now comes the challenge of implementing it again but in Java
. The reason for this is simple. Data structures are language agnostic. If my understanding is at a decent level I should be able to implement a simplistic version of the data structure in any language I am somewhat familiar with. At least that is the hope!
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 added the last improvement to the Singlely Linked List I wrote. The addition was a function that adds a new node after an existing node in the list. I guess this would be considered an insertion process and I am aware that insertions are none to be fairly fast with linked lists. Thankfully writing this function made total sense to me and there were no
C
quirks that I had to learn before I could understand it. Now comes the task of doing this again but in Java. I hope there aren’t too many hiccups along the way.
Improvement Number 7
// Improvement 7
// Add a function that allows us to insert a new node after
// another node.
#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;
}
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 insert_after_node(node_t *node_to_insert_after, node_t *new_node){
new_node->next = node_to_insert_after->next;
node_to_insert_after->next = new_node;
}
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);
// Add a new node after the found node
insert_after_node(tmp, create_new_node(1000));
printList(head);
return 0;
}
The output is the same as the previous programs:
9 - 8 - 7 - 6 - 5 - 1000 - 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.