An implementation of a linked list in Java

 Linked List

A linked list is a data structure consisting of nodes. Each node has a variable pointing to the node next to it. This makes the linked list a way of storing data in a sequence. This may sound similar to an array. It is. However, an array has a fixed size, making insertion a little trickier. With a linked list, the number of nodes can change to however many we want and insertion is as easy as changing the pointing variable of the node before which you would like to insert the new node. 

Linked lists can also be used to implement more complex data structures such as trees and graphs. 

An interest in the data structure of linked lists in general and a new introduction to the Java programming language sparked my interest in implementing this in Java. Having done some projects in C++, I had already begun thinking up a way to do this in C++. But doing it with Java, presented some new learnings to me, mainly concerned with the structure of the program and the way Java files are executed in VS Code.


Setting up the classes:

I used classes to implement a linked list. There is a Node class and a LinkedList class to handle the nodes. Nodes contain information and a pointing variable to the next node. The LinkedList class contains methods to insert more nodes and display the entire sequence together.

I created a single java file named "linked_list.java" and another "test.java" to test the linked list.
The file linked_List.java contained the Node and LinkedList classes. The test.java file contained the test class which had code used to test the methods from the LinkedList class.

Note: The class in test.java does not need to have the same name as the name of the file.

This is "linked_list.java"

class Node {
    int value;                     //data stored in the node
    Node next;                     //pointing variable aka reference to the next node

    Node(int value) {
        this.value = value;               //constructor for node
    }
}

class LinkedList {
    Node head;
    
    LinkedList() {
        this.head = null;                 //constructor for a linked list
    }
    
    public void insert(int value) {
        Node newNode = new Node(value);
        if(head == null) {
            head = newNode;                  //if list is empty
        }
        else {
            Node currentNode = head;
            while(currentNode.next != null){
                currentNode = currentNode.next;
            }
            currentNode.next = newNode;              //link the newNode to the last node
        }
    }

    public void print() {
        Node currentNode = head;              //begin at head
        while(currentNode != null) {
            System.out.print(currentNode.value + " -> ");
            currentNode = currentNode.next;
        }
        System.out.println("null");
    }
}

This code is compiled using the terminal by typing "javac linked_list.java" and pressing enter. 

Setting up the test file:

The test.java file was created to demonstrate the methods of the LinkedList class being used. The LinkedList class could only be accessed because we created an object of that class. Both the "test.java" and "linked_list.java" were located in the same directory.

Here is the code for test.java

class test {
    public static void main(String[] args) {
        LinkedList demonstration = new LinkedList();

        demonstration.insert(2);
        demonstration.insert(4);
        demonstration.print();
    }
}

The file is compiled by typing "javac test.java" in the terminal (which was opened in the directory where these files were located). It was executed by typing "java test". (Note: Here after java we must type the name of the class that we want to run, not the name of the file in which it is defined.).

Here is the output of test:


Scope

I will probably be using the linked list to create a simple project soon. Just to see what I could do with it. A linked list is also useful in creating other data structures. It can be used to manage objects with dynamic behavior as insertion is easy.

Comments

Popular posts from this blog

Using a CSV file from C++ program | Flight booking system

Flight booking system in C++