Make a custom generic linked list in Java

In a previous post, we learned how to made a custom generic linked list using the iteration approach. In this post, we will learn how to make a generic custom linked list with some custom methods which will follow the java object model.

You must be having many questions in your mind like –

How to make a generic linked list?
How to make our internal node structure?
How to keep track of the head node?
How to add new nodes in the linked list?
How to print a linked list?

All these questions will be answered in the below post. So, let us go one by one-

How to make a generic linked list?

Well, the answer is easy, we will be using the generic feature of java and define our class in the following way –

public class LinkedList<T> {

}

and then by making all methods like add, remove accepting the Type T value in the arguments.

How to make our internal node structure?

Our node will contain mainly of 2 fields –

A data field ( to store the actual data).
A next field ( to point to the next node).

Below is the structure for the same –

class Node {
    private T value;        // the data value of type T
    private Node next;     // to point to the next node

    //constructor
    private Node(T value) {
        this.value = value;
    }
}

How to keep track of the head node?

Now, after making our linked list class generic and describing the node structure, the next thing is to know that how we will keeping track of our head node while making a linked list. A head node is the first node in the linked list.

We will make a new data member in our Linked List class named head of type Node.

private Node head;

How to add new nodes in the linked list?

For this purpose, we will make a function named add
add(T element) – It will accept the value to be added as an argument and it will be of Type T as we are making a generic Linked List.

There is a need for special handling while adding our first node, as we have to set the head node in those cases and otherwise not. In case, when the head node is set, we have to go to the last node and add a new node there. Below add function depicts the same –

void add(T element) {
	Node node = new Node(element);   // making a new node
	if (this.head == null) {         // if head node is null, making current node as head node
		this.head = node;          
	} else {
		Node iterator = this.head;  
                // iterating over to the last node 
		while (iterator.next != null) {
			iterator = iterator.next;
		}
		iterator.next = node;     // adding a new node 
	}
}

How to print a linked list?

Well, printing a linked list is also easy as we just have to iterate over the nodes until there is no node left. Below is the code for the same –

void print() {
    Node iterator = head;
    while (iterator != null) {
        System.out.println(iterator.value + " ");
        iterator = iterator.next;
    }
}

Now, after understanding the various components. It is time to look at the whole code. So, refer to the below one for that –

public class LinkedList<T> {

	private Node head;

	class Node {
		private T value;
		private Node next;

		private Node(T value) {
			this.value = value;
		}
	}

	void add(T element) {
		Node node = new Node(element);
		if (this.head == null) {
			this.head = node;
		} else {
			Node iterator = this.head;
			while (iterator.next != null) {
				iterator = iterator.next;
			}
			iterator.next = node;
		}
	}

	void print() {
		Node iterator = head;
		while (iterator != null) {
			System.out.println(iterator.value + " ");
			iterator = iterator.next;
		}
	}

	public static void main(String[] args) {

		LinkedList<Integer> integerLinkedList = new LinkedList<>();
		integerLinkedList.add(10);
		integerLinkedList.add(20);
		integerLinkedList.add(30);
		integerLinkedList.print();

		LinkedList<String> stringLinkedList = new LinkedList<>();
		stringLinkedList.add("Hello");
		stringLinkedList.add("Codekru");
		stringLinkedList.print();
	}

}

If you want to know about the linked list implementation of java and its methods or functions, then we suggest you visit this link.

I hope you find this helpful. If you have any doubts or concerns, you can write to us in comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.