How To Create Own Linked List Class In Java?

Inayat Ali
3 min readNov 3, 2019

What is Linked List?

Linked List are linear data structures where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays.

Dis Advantage:

It also has few disadvantages like the nodes cannot be accessed directly instead we need to start from the head and follow through the link to reach to a node we wish to access.

Constructors for Java Linked List:

  1. LinkedList(): Used to create an empty linked list.
  2. LinkedList(Collection C): Used to create a ordered list which contains all the elements of a specified collection, as returned by the collection’s iterator.

You also create your own constructor.

How To Store Element in it?

To store the elements in a linked list we use a doubly linked list which provides a linear data structure and also used to inherit an abstract class and implement list interfaces.

How to create Understand it by coding?

First of all create an class Name as Node. Now create a variables, first one to store data (instead of datatype , It depend on your use or use Object class variable to store in general form) and second one is the reference variable of same class Node.

As shown below:

public class Node {
Object date;
Node next;
}

// Node class ends

Now class it in LinkedList class which performs function and store elements, next element reference in Node class.

Now in LinkedList class there are few functions , which are doing same as the builtin function are doing.

public void insert(int date); // This is a function used to insert an element.

public void insertAtStart(int data); // This is a function to insert an element in the start.

public void insertAt(int index,int data); // this function take two argument first the position where to store and second one the value which has to store.

public void deleteAt(int index); // this function is used to delete any element on the given index.

public void show(); // this function is used to print whole Linked List.

The program is given below:

public class LinkedList {
Node head;
public void insert(int date)
{
Node node=new Node();
node.date=date;
node.next=null;
if(head==null)
{
head=node;
}
else
{
Node n=head;
while(n.next!=null)
{
n=n.next;
}
n.next=node;
}
}
public void insertAtStart(int data)
{
Node node =new Node();
node.date=data;
node.next=null;
node.next=head;
head=node;
}
public void insertAt(int index,int data)
{
if (index==0)
{
insertAtStart(data);
}
else
{
Node node=new Node();
node.date=data;
node.next=null;
Node n=head;
for(int i=0;i<index-1;i++)
{
n=n.next;
}
node.next=n.next;
n.next=node;
}
}
public void deleteAt(int index)
{
if (index==0)
{
head=head.next;
}
else
{
Node n=head;
Node n1=null;
for(int i=0;i<index-1;i++)
{
n=n.next;
}
n1=n.next;
n.next=n1.next;
}
}
public void show()
{
Node n=head;
while(n.next!=null)
{
System.out.println(n.date );
n=n.next;
}
System.out.println(n.date);
}
}

Now you can call it in main function by using object of class and assign elements to it.

--

--