Showing posts with label linked list. Show all posts
Showing posts with label linked list. Show all posts

Complete Linked List with operations

Complete Linked List with operations
We have discussed the various concepts including the operations of Linked List in our previous post. So if you are unaware of these concepts, please visit our previous tutorials

Lets look at the complete Linked List creation in Java with all the operations we have discussed.
Here is the Java Code.
import org.omg.Messaging.SyncScopeHelper;

public class LinkedList {

 static LinkedNode head =  null;
 public static void main(String[] args) {
  LinkedList linkedList = new LinkedList();
  LinkedNode node = new LinkedNode(1);
  linkedList.addNode(node);
  LinkedNode node2 = new LinkedNode(2);
  linkedList.addNode(node2);
  LinkedNode node3 = new LinkedNode(3);
  linkedList.addNode(node3);
  System.out.println("After adding nodes, the list is -");
  linkedList.printList();
  int search = linkedList.searchNode(node3);
  System.out.println("Node 3 found in index " + search);
  linkedList.deleteNode(new LinkedNode(2));
  System.out.println("After deleting node 2 if present, the list is -");
  linkedList.printList();

 }

 private void deleteNode(LinkedNode node2) {
  if(head == null){
   System.out.println("List is empty");
   return;
  }
  if(head.value == node2.value){
   head = head.next;
   return;
  }else{
   LinkedNode temp1 = head;
   LinkedNode temp = head.next;
   while(temp != null && temp.value != node2.value ){
    temp1 = temp;
    temp = temp.next;
   }
   if(temp == null){
    System.out.println("The node to be delete is not found ");
   }else{
    temp1.next = temp.next;
   }
  }
 }

 private void printList() {
  if(head != null){
   LinkedNode temp = head;
   while(temp != null){
    System.out.print(temp.value + " ");
    temp = temp.next;
   }
   System.out.println();
  }else{
   System.out.println("The List is empty, Please add node elements first");
  }


 }

 private int searchNode(LinkedNode node2) {
  int position = 0;
  if(head == null){
   System.out.println("List is empty");
   return -1;
  }
  if(head.value == node2.value){
   System.out.println("Node found a position " + position);
   return -1;
  }else{

   LinkedNode temp = head;
   while(temp != null && temp.value != node2.value ){
    position++;
    temp = temp.next;
   }
   if(temp == null){
    System.out.println("Node found a position " + position);
    return -1;
   }
  }
  return position;
 }



 private void addNode(LinkedNode node) {
  if(head == null){
   head = node;
   return;
  }
  else{
   LinkedNode temp = head;
   while(temp.next != null){
    temp = temp.next;
   }
   temp.next = node;
  }
 }

}

class LinkedNode {
 protected int value;
 protected LinkedNode next;
 public LinkedNode(int value) {
  this.value = value;
  this.next = null;
 }

}

//output
//After adding nodes, the list is -
//1 2 3 
//Node 3 found in index 2
//After deleting node 2 if present, the list is -
//1 3 

That's all for Single Linked List. We will discuss about double linked list and circular linked list as part of our next tutorials. We will also discuss various interview questions related to these concepts.

Hope you like it. Please comment on any doubts.
Happy Learning !!!

Displaying the Linked List

Displaying the Linked List
Here we will write code to display or print the Linked List.

As we have discussed the last node's next link to null, we can utilize this to reach the end of the list and display its contents while traversing.

Below is the Java code.

   private void printList() {
        if(head != null){
            LinkedNode temp = head;
            while(temp != null){
                System.out.print(temp.value + " ");
                temp = temp.next;
            }
            System.out.println();
        }else{
            System.out.println("The List is empty, Please add node elements first");
        }
        
        
    }
Here temp = temp.next; traverse the list one by one until it points to null.
Hope you like it. Please comment for doubts.

Happy Learning !!!

Searching for a node in Linked List

Searching for a node in Linked List
Searching for a node in Linked List
Given a node data, we will find the position of the node in the list if present counting from 0.
As we know the last node's next points to null, we will loop the list till the next of node points to null and check each node data with the given node's data.

Below is a Java code to find the position of a node.


private void searchNode(LinkedNode node2) {
 int position = 0;
 if(head == null){
            System.out.println("List is empty");
            return;
        }
        if(head.value == node2.value){
            System.out.println("Node found a position " + position);
            return;
        }else{
            
            LinkedNode temp = head;
            while(temp != null && temp.value != node2.value ){
  position++;
                temp = temp.next;
            }
            if(temp == null){
                System.out.println("Node found a position " + position);
            }else{
                temp1.next = temp.next;
            }
        }
 }

Hope you like it. Please comment for doubts.
Happy Learning !!!

Deletion of a Node in Linked List

Deletion of a Node in Linked List
Delete a node given a index in Linked List

Here we will delete a node from the list given the node data to be deleted.
We will maintain two links here so as to link the previous node's next to the deleted node's next.
Image result for deletion of a node in linked list
In the above diagram node to be deleted is 99, so we will link node 12 next to node 37 whose link we can have from node 99 next.

Below is a Java code.
 private void deleteNode(LinkedNode node2) {
  if(head == null){
            System.out.println("List is empty");
            return;
        }
        if(head.value == node2.value){
            head = head.next;
            return;
        }else{
            LinkedNode temp1 = head;
            LinkedNode temp = head.next;
            while(temp != null && temp.value != node2.value ){
                temp1 = temp;
                temp = temp.next;
            }
            if(temp == null){
                System.out.println("The node to be delete is not found ");
            }else{
                temp1.next = temp.next;
            }
        }
    }
Node temp is the node to be deleted and node temp1 points to the previous node to be deleted. Hope you like it. Please comment on doubts.

Happy Learning !!!

Addition of Node in Linked List

Addition of Node in Linked List
We will be discussing the various operations of Linked List we mentioned in our previous post.
The basic operations performed in a Linked List are
  • Addition of a node.
  • Deleting a node.
  • Searching for a node.
  • Display the Linked List.
Addition of a node to the list.

Here we will be adding a node to the beginning of the list and make the head point to the newly added node. Below is a code to add at the beginning of the list.

    
private void addNode(LinkedNode node) {
        if(head == null){
            head = node;
            return;
        }
        else{
            LinkedNode temp = head;
            while(temp.next != null){
                temp = temp.next;
            }
            temp.next = node;
        }
    }
We will discuss the other operations in coming tutorials.
Hope you like it. Please comment on doubts.
Happy Learning !!!

Initialize a Linked List

Initialize a Linked List
Let see how to initialize a LinkedList. Here we will be initializing a Integer Linked list storing int value as datatype.
LinkedList is a class representing the list, LinkedNode is represented as a separate class. If you are unaware of the terms, please refer to our previous tutorial introduction to linked list.

class LinkedNode {
    protected int value;
    protected LinkedNode next;
    public LinkedNode(int value) {
        this.value = value;
        this.next = null;
    } 
}

public class LinkedList {

    static LinkedNode head =  null;
    public static void main(String[] args) {
 System.out.println("Initialize a LinkedList");
        LinkedList linkedList = new LinkedList();
        LinkedNode node = new LinkedNode(1);
 LinkedList linkedList = new LinkedList();
        LinkedNode node = new LinkedNode(1);
        linkedList.addNode(node);
        LinkedNode node2 = new LinkedNode(2);
        linkedList.addNode(node2);
        LinkedNode node3 = new LinkedNode(3);
        linkedList.addNode(node3);
        System.out.println("After adding nodes, the list is -");
        linkedList.printList();
        linkedList.deleteNode(new LinkedNode(2));
        System.out.println("After deleting node 2 if present, the list is -");
        linkedList.printList();
 }
}
Here LinkedNode is the node for the Linked List to be created. It contains int as data and a next link which is of datatype LinkedNode to link to the next node of the list.

 LinkedList is the list class we are interested to create. It has a static variable head initialize to null which always points to the head node of the list. We initialize the list in the line LinkedList linkedList = new LinkedList();  and initialize a node in LinkedNode node = new LinkedNode(1); We will explain why head is made as a static variable in next tutorials.
LinkedList class have functions of adding a node to the list, deleting a node from the list and printing the list. We will explain all these methods in our next tutorials.

That's all of initializing a LinkedList. Hope you like it. Please comments for any doubts.

Happy Learning !!!

Introduction to Linked List

Introduction to Linked List
Introduction

Linked List is a linear data structure (Abstract Data Type) just like arrays. Unlike arrays, linked list elements are not stored at contiguous location; the elements are linked using pointers.

Below is a pictorial representation of linked List. 

linkedlist
Within the list, a 'node' is a structure comprising fields that store data, with addition of a 'link' field to the location of the next record. Each record is created dynamically as and when required. The location of the first record is held in a variable which points to head or start of the list. The end of the list is normally marked by a special null value in the final link field. Exception in case of Circular Linked List. We will discuss this as a whole in separate topic.

So Why Linked List and not Arrays?
  1. Linked List size is not fixed like array. We need not worry for exception like IndexOutOfBoundException while using Linked List as its dynamic in nature.
  2. Addition and Insertion is costly for arrays as we have to move all the elements in both case. In case of List we can add or remove an element at ease using links.
Drawbacks
Can't access elements randomly, Have to access in a sequential manner.
Extra memory space required for the link field.

Hope you like it.
Happy Learning !!!

How to Calculate Length of LinkedList

How to Calculate Length of LinkedList
SOURCE : LengthLinkedList.java


/**
* @author sandeep
*
*/
public class LengthLinkedList {

// for this use ListNode Class given in this Tutorial
private static int ListLength(ListNode headNode) {

int length = 0;
ListNode CurrentNode = headNode;
System.out.print("LinkedList::::: ");
while (CurrentNode != null) {
System.out.print(CurrentNode.getData()+"->");
length++;
CurrentNode = CurrentNode.getNext();
}
System.out.print("null");
return length;
}

public static void main(String[] args) {
ListNode ls = new ListNode(10);
ListNode ls0 = new ListNode(20);
ListNode ls1 = new ListNode(30);
ListNode ls2 = new ListNode(40);
ListNode ls3 = new ListNode(50);
ls.setNext(ls0);
ls0.setNext(ls1);
ls1.setNext(ls2);
ls2.setNext(ls3);

// length of linked List
System.out.println(" Length of LinkedList before Inserstion::: " + ListLength(ls));

}

}


INPUT

LinkedList::::: 10->20->30->40->50->null



OUTPUT

Length of LinkedList before Inserstion::: 5







Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+