Skip to main content

Data Structure: Single LinkedList in Java

Linked List:

Linked List contains a sequence nodes which are linked together. Each node contains a connection to another link and data. Linked list is the second most-used data structure after array. Following are the important terms to understand the concept of Linked List.


  • Link − Each link of a linked list can store a data called an element.
  • Next − Each link of a linked list contains a link to the next link called Next.
  • LinkedList − A Linked List contains the connection link to the first link called Head.


Types of Linked List:

Following are the various types of linked list.
  • Simple Linked List − Item navigation is forward only.
  • Doubly Linked List − Items can be navigated forward and backward.
  • Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous.

Basic Operations:

  1. Insert: Inserts at tail, specific index.
  2. Delete: Deletes from the tail. specific index.
  3. Display: Displays the List.
  4. Search: Search List for a specific element.

Single Linked List:




Insertion Operation:
Adding a new node in the linked list is a more than one step activity. We shall learn this with diagrams here. First, create a node using the same structure and find the location where it has to be inserted.















Implementation of Single LinkList in Java:


Node Class:


public class Node 
{
    protected Node next;
    protected int data;
   
    public Node()
    {
        next=null;
        data=-1;
    }
   
     public Node(int a)
    {
        data=a;
    }
    
    public void Display()
    {
        System.out.println(data+" ");         
    }
}





Link list Class:


public class linklist
{
    protected Node Head;
   
    public linklist()
    {
        Head=null;
    }
    public linklist(int a)
    {
        addFirst(a);
    }
    public void addFirst(int a)
    {
        Node newNode=new Node(a);
        if(Head==null)
        {  
            Head=newNode;         
        }
        else
        {
            newNode.next=Head;
            Head=newNode;
        }
    }
   
    public void addLast(int a)
    {
        if(Head==null)
        {
            addFirst(a);
        }
        else
        {
            Node curr=Head;
           
            while(curr!=null)
            {
                if(curr.next==null)
                {
                    Node newNode =new Node(a);
                    curr.next=newNode;
                    break;
                }
                curr=curr.next;
            }
        }
       
    }
    public boolean isEmpty()
    {
        return Head==null;
    }
    public void deleteFirst()
    {
        if(Head==null)
            System.out.println("List Is Empty.");
        else
            Head=Head.next;
    
    }
    public void deleteLast()
    {
        Node curr=Head;
        Node prev=Head;
        if(Head==null)
        {
            System.out.println("List Is Empty.");
        }
        else
        {
            while (curr.next!=null)
            {
                prev=curr;
                curr=curr.next;
            }
            prev.next=null;
            prev=null;
        }
    }
    public boolean searchElement(int a)
    {
        Node curr=Head;
        while (curr!=null)
        {
            if(curr.data==a)
                return true;
            curr=curr.next;
        }
        if (curr==null)
        {
            return false;
        }
        else
            return true;
    }
    public void searchInsert(int search,int d)
    {
        Node newNode=null;
        Node temp;
        Node curr=Head;
        while (curr!=null)
        {
            if(curr.data==search)
            {
                newNode=new Node(d);
                temp=curr.next;
                curr.next=newNode;
                newNode.next=temp;
            }
           curr=curr.next;
        }
        if(newNode==null)
            System.out.println("Position or Element Not Found");
    }
   
    public void searchDelete(int search)
    {
        Node curr=Head;
        Node prev=null;
        while (curr!=null)
        {
            if(curr.data==search)
            {
                if(curr.next==null)
                    deleteLast();
                else if(curr==Head)
                    deleteFirst();
                else
                prev.next=curr.next;
            }
            prev=curr;
            curr=curr.next;
        }
        if(prev==null)
            System.out.println("Position or Element Not Found");
    }
 public void Print()
 {
    Node curr=Head;
    if (Head==null)
    {
        System.out.println("List Is Empty");
    }
    else
    {
        System.out.print("List: ");
        while (curr!=null)
        {
            curr.Display();
            curr=curr.next;
        }
        System.out.println();
    }
 }
   
  
}

Main:


    public static void main(String[] args) {
       
        linklist x=new linklist(1);
        x.addFirst(3);
        x.addFirst(5);
        x.addFirst(8);
        x.Print();
       
        x.addLast(9);
        x.addLast(7);
        x.Print();
       
        System.out.println(x.searchElement(35));
       
        x.deleteFirst();
        x.deleteFirst();
        x.Print();
    }

 





Comments

Popular posts from this blog

Assembly Language: Configuration of Visual Studio

Setting up Visual Studio: Introduction of Microsoft Visual Studio and configure it for Assembly Language Programming.  Introduction to Visual Studio: Visual Studio (for all its versions) is an integrated development environment (IDE) from Microsoft. It is a leading tool to develop computer programs, as well as web sites, web applications and web services.We could, however, use a stand-alone assembler like NASM or MASM to code in Assembly Language. Here is the configuration of Microsoft Visual Studio 2010 and provided below is the link of my YouTube channel that is showing the configuration of Microsoft Visual Studio 2012. The configuration of all the versions is SAME. Link:  https://youtu.be/SjZLtfgZ8tk    Configuring Visual Studio for Assembly Language: Install the latest version (for 6th/7th edition) of Irvine’s link library from the link given below  http://kipirvine.com/asm/examples/index.htm    Once you have downloaded the req...

Data Structures: Primitive Operations

Primitive Operations  Primitive operations are the building blocks of the algorithm in the Data Structures. It help us to count the operations in any certain algorithm to calculate the time complexity of the algorithm. What is Data Structure? Data Structure is the systematic way of accessing and organizing data. What is Algorithm? Algorithm is the step by step procedure to perform the task in the finite amount of time. You might be thinking why is there a need to calculate the time complexity of the algorithm. Technically talking being a Computer Science student, it is too much necessary to calculate the run time to check the efficiency of the algorithm. The analysis is performed on the two basic factors that are: Run time  Space Usage There are two types of the analysis: Experimental Analysis Theoretical Analysis Experimental analysis is performed on the finite amount of data while the theoretical analysis is performed on all types of data so we can ...