Skip to main content

Single LinkList 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 First.


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<T>
{
    protected Node<T> next;    protected Node<T> prev;    protected T data;
    public Node()
    {
        next=null;    }

    public Node(T a)
    {
        data=a;    }

    public void Display()
    {
        System.out.print(data+"  ");    }
}

LinkList Class:

public class LinkList<T>
{
    protected Node<T> Head;    protected int size=0;
    public LinkList()
    {
        Head=null;    }

    public LinkList(T a)
    {
        addFirst(a);    }

    public void addFirst(T a)
    {
        Node list=new Node(a);
        if(Head==null)
        {
            Head=list;        }
        else        {
            list.next=Head;            Head=list;        }

        size++;    }

    public void addLast(T a)
    {
        Node curr=Head;
        if(Head==null)
        {
            addFirst(a);        }
        else        {
            while(curr!=null)
            {
                if(curr.next==null)
                {
                    Node newNode=new Node(a);                    curr.next=newNode;                    break;                }
                curr=curr.next;            }
        }
        size++;    }

    public boolean isEmpty()
    {
        return Head==null;    }

    public void deleteFirst()
    {
        if(Head==null)
            System.out.println("List Is Empty.");        else            Head=Head.next;
        size--;    }

    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;        }

        size--;    }

    public boolean searchElement(T 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(T search,T 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");
        size++;    }

    public void searchDelete(T 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");
        size--;    }


    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();        }

    }

}





For Video Tutorial visit:https://youtu.be/P7DFX-JJ59c





Comments

Popular posts from this blog

Stack Data Structure in Cpp (c++)

  What is Stack? Stack refers to an abstract data types (ADTs) that is a collection of elements, with two principal operations that are addition and removal of elements from the collection. It works on the principle of LIFO (last In first out). It is also considered as a linear data structure.   Example: A set of books. It easy to take an item off the top rather from the middle. Basic operations of Stack: Push: Add item to the list. Pop: Removes item from the list.   Advantages: Stack is faster than heap memory allocation (also known as Dynamic Memory Allocation). Stack memory is automatically reclaimed when the function exits, which can be convenient for the programmer if the data is no longer required. Therefore, it is suitable for temporary data, where memory is not needed after the execution of a function. Implementation Of Stack in C++: #include<iostream> using namespace std; class StackMemory...

Tic-Tac-Toe Implementation in Cpp (C++) Step by Step

Inheritance (OOP)

Inheritance: In object-oriented programming,  inheritance  means an object or class has inherited behaviors from another object  or class. A child class is inherited from parent class. A child inherits characteristics of its parents, besides  inherited characteristics, a child may have its own unique characteristic. . In inheritance, every object of a child class is also an object of parent class, but not vice-versa. Base class: It is the class from which features are to be inherited into another class. Derived class: It is the class in which the base class features are inherited. A derived class can have additional properties and methods not present in the parent class that distinguishes it and provides additional functionality. REAL WORLD EXAMPLE: Let’s consider the Windows operating system. Windows 98 had certain properties and methods that were used in Windows XP. Windows XP was derived from Windows 98, but it was sti...