Posts which you will also like.

Monday, January 23, 2012

Linked List Implementation In Java


In this post I will develop a java program to implement linked list from scratch.Creating a linked list in java is much easier than creating it in c or c++.
Follow the steps to create a linked list:
1.Create a class LinkedList with a private static inner class which will be the node of linked list or you can also create a separate class Node instead of inner class.But I think a private Inner class will be best suitable for encapsulation property(Make it private if your node is not going to be used for another purpose).
2.Node class has two attributes Light bulb
first information and second link which stores the address of next node in the chain.
3.Define suitable constructor for Node class
4.Define getters and setters for class if you feel need for it.
5.Now proceed back to your LinkedList class. It has a single attribute of Node class and add functionality to your LinkedList like methods to append, add at beginning ,insert at any point, delete,display etc.
6. Now your linked list is complete !!! You can use it any where you like.blogpost_03

Source Code :
   1:  package com.techy.rajeev;


   2:   


   3:  public class LinkedList {


   4:      private Node start;


   5:      public void insert(int x){


   6:          if(start==null)


   7:          start=new Node(x,start);


   8:          if(start!=null){


   9:              Node temp=start;


  10:              while(temp.next!=null){


  11:                  temp=temp.next;


  12:              }


  13:              temp.next=new Node(x);


  14:          }


  15:      }


  16:  public void display(){


  17:          if(start==null){


  18:              System.out.println("List is empty!");


  19:          }else{


  20:              Node temp=start;


  21:              while(temp.next!=null){


  22:                  temp=temp.next;


  23:                  System.out.println(temp.getData());


  24:              }


  25:          }


  26:      }


  27:      /**


  28:       * @param x


  29:       */


  30:  public void delete(int x){    


  31:          Node prev=start;


  32:          Node temp=start;


  33:          while(temp.getData()!=x){


  34:              if(temp.next==null){


  35:                  System.out.println("Element not found!!");


  36:                  break;


  37:              }


  38:              prev=temp;


  39:              temp=temp.next;


  40:          }


  41:          if(temp==start){


  42:              start=start.next;


  43:          }else{


  44:              prev.next=temp.next;


  45:          }


  46:          


  47:      }


  48:   


  49:   


  50:      private static class Node{


  51:          private int data;


  52:          Node next;


  53:          


  54:          Node(int data){


  55:              this.setData(data);


  56:          }


  57:          Node(int data,Node next){


  58:              this.setData(data);


  59:              this.next=next;


  60:          }


  61:          public int getData() {


  62:              return data;


  63:          }


  64:          public void setData(int data) {


  65:              this.data = data;


  66:          }


  67:   


  68:      }


  69:   


  70:  }




Now the class in which this simple linked list is used.


   1:  public class MainClass {


   2:      public static void main(String args[]){


   3:          LinkedList l=new LinkedList();


   4:          l.insert(1);


   5:          l.insert(2);


   6:          l.insert(3);


   7:          l.insert(4);


   8:          l.insert(5);


   9:          l.display();


  10:          System.out.println("-------------------------");


  11:          l.delete(2);


  12:          System.out.println("---------after deletion---------");


  13:          l.display();


  14:      }


  15:  }





In some other post I will use the generics to implement much robust linked list.

More on this at techcresendo.com

2 comments:

  1. while(temp.next!=null){


    22: temp=temp.next;


    23: System.out.println(temp.getData());


    24: }

    in Line while loop use temp != null, instead of testing for temp.next ! and place the temp=temp.next; after the print statement. u want to first read the data and then increment it.

    ReplyDelete

Your comment may wait for moderation....

DMCA.com Protected by Copyscape Online Plagiarism Tool