Linked List

Data Structure Java

Okay so first, what is a data structure?... to put it in the easiest way to understand, a data structure is simply a way of organizing data. Data structures are important because they allow us to use a computer's memory in the most efficient way possible.

And this is how a computer's memory works: Computer memory is a temporary storage area. It holds the data and instructions that the Central Processing Unit (CPU) needs. Before a program can run, the program is loaded from storage into the memory. This allows the CPU direct access to the computer program.

It is very important that as a developer you have some level of understanding for this ''scary'' concept called data structures and algorithms. Because it helps us to write code in the most logical way possible.

As a beginner, the most common data structure you learn about is the Array. An array is a list of related data all organized in one variable.

public class Array{
public static void main(String[] args){

     String colors = "red";
     String colors = "blue";
     String colors = "green";

     // logical way of organizing the variables above
     String [] colors = {"red", "blue", "green"};

  }        
}

We all know that you can randomly access an element in an array via its index and that is one of the advantages of the array data structure. The problem with arrays is that when working with a significantly larger dataset than the example above, inserting and deleting elements within the array takes a lot more time making the program run less efficiently (using up more memory), and that is something we don't want. And that's why we have more data structures to handle these processes more efficiently.

This is where the Linked List data structure comes in.

public class LinkedList{
      public static void main(String[] args){
        LinkedList<String> list = new LinkedList<String>();
}
}

A linked list is a chain of nodes where each node has two parts, one part is the data it holds and the second part is the address (pointer) location of the next node in line. This type of linked list is referred to as a singly linked list. There is a single link to each node. Linked lists don't have indexes the same way arrays do, therefore the data is randomly organized within your computer's memory (the computer only stores the memory location of the first and last node.). A linked list is a chain of nodes because each node (holding an address) knows where the next node is. They consist of a head node and a tail node, to traverse through the singly linked list you need to start from the head node and not vice versa (you can't begin at the tail node to search for data). Inserting a node in a linked list is relatively easy because you don't have to move/shift other elements. When you need to insert a new node, you take the pointer (address) of the previous node and give it the address of the new node, so that the new node is pointing to the next node in line.

Searching for elements is where linked lists are inferior compared to the array, linked lists are bad at searching (when working with large sets of data). There is no random access to locate elements, you always need to start at the head node and that takes time. Linked lists can also be treated as a Stack, meaning they have the pop() and push() methods. And they can also be treated as a Queue so they have the offer() and poll() methods.

public class LinkedList{
      public static void main(String[] args){
        LinkedList<String> listOfColors = new LinkedList<String>();
        // as a stack
        listOfColors.push("red");
        listOfColors.push("blue");
        listOfColors.push("green");
        listOfColors.push("orange");
        listOfColors.pop(); // removes the top most element... orange

         // as a queue
        listOfColors.offer("red"); // head
        listOfColors.offer("blue");
        listOfColors.offer("green");
        listOfColors.offer("orange"); // tail
        listOfColors.poll(); // removes head        

        }
}

Inserting and Deleting nodes is where linked lists shine!

public class LinkedList{
      public static void main(String[] args){
        LinkedList<String> listOfColors = new LinkedList<String>();

        listOfColors.offer("red"); // head
        listOfColors.offer("blue");
        listOfColors.offer("green");
        listOfColors.offer("orange"); // tail
        // inserting
        listOfColors.add(3, "yellow"); // add yellow to listOfColors
        // deleting
        listOfColors.remove("yellow"); // removes yellow                 


        }
}

Again linked lists have the advantage over arrays when it comes to inserting and deleting nodes, but remember we still need to traverse the entire linked list to complete this process of deletion and insertion because there is no random access to the list.

These are other linked list methods that you should know about:

.indexOf(object) searches for an element.

.peekFirst() shows the head node and what it contains.

.peekLast() shows the last node and what it contains.

.addFirst(object) adds a new node to the head.

.addLast(object) adds a new node to the tail.

.removeFirst() removes the first node.

.removeLast() removes the last node.

Thanks for reading :)

*hits publish