How to use Linked List In Java Without Using Collection Class

This tutorial will show you how to use Linked List in Java without using Collection class with an easy example.

A Collection class consists mainly of static methods that return or works with collections. It is  invoked by the following statement at the beginning of the program:

public class Collections
extends Object

So let us see how can we create a linked list using collection class in java. Creating linked lists with collection class has the following advantage that you’ll be able to use predefined functions.

The following program suggests the use of the collection class to create a linked list. We are using this program to clearly identify the differences between the linked list using collection and linked list without collection class.

Program to use Linked List with “collection”:

import java.util.*; 
class Link 
{ 
  public static void main(String args[]) 
  { 
    // We are creating an object of class linked list 
    LinkedList<String> linkobj = new LinkedList<String>();  //This is a predefined statement if we want ot declae a linked list 

    // Adding elements to the object we have just created  
    linkobj.add("A"); // We are using predefined functions here provided by the collection class
    linkobj.add("B");
      	System.out.println("Linked list : " + linkobj);  
        } 
} 

The output of the above program:

How to use Linked List In Java Without Using Collection Class

 

As we can see if we use the collection class it becomes much easier to use the linked list as it already has inbuilt functions to perform operations like add, remove, count, check and replace its elements.

Now we shall see how to use a linked list without using collection class.

Program to use the linked list without collection class:

public class linkedlist
{
    public static class Node         //We are defining a node  here
    {
        int data;       //data of node here
        Node next;      //next refers to the address of the next node
        
    }
    public static void main(String args[])
    {
        Node newnode1=new Node();    //here we are making objects of class Node
        Node newnode2=new Node();    //basically these are nodes of first linkedlist
        Node newnode3=new Node();    
        Node newnode4=new Node();    
        newnode1.data=1;
        newnode2.data=2;
        newnode3.data=3;
        newnode4.data=4;
         
          
        newnode1.next=newnode2;       //here we are linking the previous node to the next node of the list 
        newnode2.next=newnode3;
        newnode3.next=newnode4;
        newnode4.next=null;

        //Now we will print the linked list

       for (int i=1;i<=4;i++)
        {
            System.out.print(newnode1.data+" ");
            newnode1=newnode1.next;       //we are assigning the next address of the linked list to the current address 
        }
    }
}

The output of the above program is:

How to use Linked List In Java Without Using Collection Class

 

So we can see using collection class is way easier than normally declaring a linked list.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *