Monday, 26 August 2013

Data Structure : Singly Linked List

Singly Linked List is the most simplest type of Data Structure. It is basically a collection of data which are linked together. Linked List are better than arrays as they can grow and shrink dynamically. Lets see the program if this.


The video explains the insert functionality of Singly Linked List


package com.singly;
class Node {
 public int data;
 public Node next;
}

public class SinglyLinkedList {

 static Node start;

 public static void main(String[] args) {

  add(10);
  add(20);
  add(30);
  add(40);
  display();

  delete(30);

  display();

 }

 private static void add(int i) {
  // Create new node
  Node p = new Node();
  p.data = i;
  // Check if List is empty, If yes than point 'start' to the newly
  // created node
  if (start == null) {
   start = p;
  } else {
   // If the list is not empty, than traverse till the end of the list
   // and add the node
   Node s = start;
   while (s.next != null) {
    s = s.next;
   }
   s.next = p;
  }

 }

 private static void display() {
  Node s = start;
  System.out.println("Data");
  while (s != null) {

   System.out.println(s.data);
   s = s.next;
  }

 }

 private static void delete(int i) {
  Node s = start;
  while (s.data != i) {
   s = s.next;
  }
  Node k = start;
  while (k.next != s) {
   k = k.next;
  }
  k.next = s.next;
 }

}


4 comments:

  1. Hi,
    I am following your blogs from some time.Thanks for the knowledge sharing.
    The above code might not work , if we try to remove the first element.Because we are not checking the first element.
    the below code might work.

    private static void delete(int i) {
    Node s = start;
    if(s.data == i){
    start = s.next;
    }else{
    while (s.data != i) {
    s = s.next;
    }
    Node k = start;
    while (k.next != s) {
    k = k.next;
    }
    k.next = s.next;
    }
    }
    }

    ReplyDelete

Share the post