-->

Linked List Basic Programming Technique

Posted by Admin on
As we know that the smallest unit of linked list is node . So first we will see what is the definition of node and how node created in java.
class Node {
    int data ;
    Node next;
 }

The above lines of codes is the definition of below . The data part of the can be of any type char , float ,String etc. As we are working in a class so we must be having constructor (for good progrmaing skills) .
So complete defintion will be as follows:-

class Node {
       int data;
       Node next;

       Node() {
              data = null;
              next = null;
       }

       Node(int d) {
              data = d;
              next = null;
       }
}

Note  :- The above  definition is not the actual node in the memory . To provide memory to the node we will have to use the following lines of code .
Node n = new Node (5);
new keyword is used to provide the memory at run time to the class references.When the above line is executed. Node is created in the memory as follows :-



Now we see the insertion algorithm :-

Insertion at the beginning :-

insertion_at_beginning(int value)
1. First we will create a new node and assign value to it.
2. If root_node==null.
    then  root=newnode
3. Else
   newnode.data=value.
   newnode.next=root.
   root=newnode.


public void insertion_at-beginning(int d)
    {
    newnode = new Node(d);
    if(root==null)
    {
        root=newnode;
     }
    else
    {
     newnode.next=root;
     root=newnode;   
}


Insertion at the end :-

insertion_at_end(int value)
1. First we will create a new node and assign value to it.
2. If  root_node==null.
    then  root=newnode.
3. Else
   1.temp node is created and assign root to it.  [  temp=root ]
   2.traverse  temp node through the linked list till the last node .
     while(temp.next!=null)
   3. assign temp node next to newnode .
       temp.next=newnode.

public void insertion_at_end(int d)
    {
    newnode = new Node(d);
    if(root==null)
    {
        root=newnode;
       
    }
    else
    {
        temp=root;
        while(temp.next!=null)
        {
            temp=temp.next;
        }
        temp.next=newnode;
    }
    }

Traversing the Linked List

 Node start=root;
        while(start!=null)
        {
            System.out.println(start.data);
            start=start.next;
        }

Deleting a node from the Linked List

Visualization of deleting of node. Suppose we have the following linked list as given in the pic . and we have to delete node 3.


delete(int num)
        {
        del_node=root;
        while(del_node!=null)
        {
            if(del_node.data==num)
            {  Node temp;
               temp= del_node.next;
               del_node.data=temp.data;
               del_node.next=temp.next;
             }
            del_node=del_node.next;
        }
          }  

Complete Program of Linked List Implementation :-

class Node 
{
    int data ;
    Node next;
    Node(int d)
    {
        data =d;
        next=null;
    }
}
class LinkedList
{
    Node root=null;
    Node newnode;
    Node temp;
    /*function to insert the element at the end */
    public void insert_at_end(int d)
    {
    newnode = new Node(d);
    if(root==null)
    {
        root=newnode;
        
    }
    else 
    {
        temp=root;
        while(temp.next!=null)
        {
            temp=temp.next;
        }
        temp.next=newnode;
    }
    }
    /*function to insert the element at the begining */
    public void insert_at_begining(int d)
    {
        newnode = new Node(d);
        if(root==null)
        {
            root=newnode;
        }
        else 
        {
          newnode.next=root;
          root=newnode;
        }
    }
    /*function to delete the element  */
    public void delete(int num)
    {
        Node del_node=root;
    while(del_node!=null)
    {
        if(del_node.data==num)
        {  Node temp;
           temp= del_node.next;
           del_node.data=temp.data;
           del_node.next=temp.next;
         }
        del_node=del_node.next;
    }
        
    }
    public void display()
    {
        
        Node start=root;
        while(start!=null)
        {
            System.out.print(start.data+"->");
            start=start.next;
        }
    }
}
 
public class LinkedListComplete {
        public static void main (String[] args)     {
    LinkedList list=new LinkedList();
    list.insert_at_end(9);
    list.insert_at_end(1);
    list.insert_at_end(8);
    list.insert_at_end(2);
    System.out.println("Traversing the linked List");
    list.display();
    list.insert_at_begining(10);
    System.out.println("\nTraversing the linked list after adding at beggining");
    list.display();
    list.delete(1);
    System.out.println("\nTraversing linked list after deletion of node");
    list.display();
    
  }
 
}

Please comment if you find any mistake in the post:



No comments:

Post a Comment