Wrote out a Singlely Linked List class in Java and wow is it an adjustment. This was the first program that I have formally written in Java and the quirks of the language were definitely throwing me off. But it was worth it to practice implementing the linked list in another language for sure. Java apparently does not have pointers but instead have references. This was much closer to what I am used to with all my experiences working with JavaScript. I can now finally move on from linked lists. I could cover Doubly Linked Lists but I just think that it would be a better opportunity for revision if I come back for that later once I have gone through everything else.
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. I completed my study of Linked Lists (for now at least). I wrote out a Singlely Linked List program in
Javawhich helped me solidify my understanding of Linked Lists.Javadoes not have pointers but rather they use references with variables of theObjecttype. This was actually my secondJavaprogram I have written. The first was of course a “Hello World!” Program. LOL. Progress I guess! Now I can move on to Trees I guess.
Linked List in Java
import java.util.*;
public class SingleList {
class Node {
int value;
Node next;
}
Node head;
void add(int newValue) {
Node newNode = new Node();
newNode.value = newValue;
newNode.next = head;
head = newNode;
}
public String toString() {
Node tmp = head;
String result = "[";
while(tmp != null) {
result = result + Integer.toString(tmp.value) + " - ";
tmp = tmp.next;
}
result = result + "]";
return result;
}
}
public class LinkedList {
public static void main(String[] args) {
SingleList singleList = new SingleList();
singleList.add(10);
singleList.add(15);
singleList.add(20);
System.out.println(singleList);
}
}
The output is the same as the previous programs:
[20 - 15 - 10 - ]
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.