LINKEDLIST PROGRAM IN JAVA

 /**  
 LinkedList.java: This Program Demonstrate how to create Node,Insert Element   
 in the linked list and display and Delete element.  
 author:itsafiz@gmail.com  
 */  
 import java.util.*;   
 class Node  
 {  
      int data;   
      Node next;   
      Node(int d)// node class Constructor.   
      {  
            data = d;   
           next= null;   
      }  
 }  
 class LinkedList  
 {  
 public static void main(String[]args)  
      {  
      Scanner in = new Scanner(System.in);  
      int data,ch,size=0;   
      Node head=null,tail=head;   
           while(true)  
           {  
           System.out.println("1.Insert\n2.Display\n3.Delete\n4.Exit");  
           ch=in.nextInt();  
                switch(ch)  
                {  
                case 1:  
                     System.out.println("Enter your Element");  
                     data = in.nextInt();   
                     Node newNode= new Node(data);   
                     if(head ==null){  
                          head=newNode;  
                          tail=head;}  
                     else{  
                          tail.next=newNode;// inserting element   
                          tail = newNode;   
                       }  
                size++;   
                break;  
                case 2:  
                     if(size>0){  
                     for(Node temp = head;temp!=null;temp=temp.next)  
                          System.out.print(temp.data + "\t");  
                          System.out.println();}  
                     else System.out.println("LinkedList is empty");  
                break;  
                case 3:  
                     if(size>0){  
                     System.out.println("Enter your element");  
                     data = in.nextInt();  
                     if(head.data==data)  
                          head=head.next;   
                     else{  
                          tail=head;   
                          Node temp=head.next;   
                          while(temp!=null)  
                          {  
                               if(temp.data==data)  
                                    {  
                                         tail.next=temp.next;   
                                         size--;   
                                         break;   
                                    }  
                               tail=temp;  
                               temp=temp.next;   
                          }  
                       }//else   
                     }else System.out.println("LinkedList is empty");  
                break;   
                default: System.exit(0);   
                }// switch  
           }  
      }  
 }  

Post a Comment

36 Comments

  1. Please give a option to download the code :) . Fantastic work.

    ReplyDelete
  2. Got the code. Had some trouble in copying. Sorry for the last comment.

    ReplyDelete
    Replies
    1. share your email id .. still you need the source file

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. @Anamica: Yes, both are instances of same class type. Node Head used to remember the Head of the linked list.

      1. Node head; // declaration

      2. Node Head = new Node(); // initialization.

      I hope it makes sense.

      Delete
  4. Hey Hi have a question...I am confused between 2 statements.....

    1)Node head
    2)Node temp=new Node()....whats does both do...Because both are creating object instances right...so whats different...Can you explain clearly...please..

    ReplyDelete
    Replies
    1. @Anamica: Yes, both are instances of same class type. Node Head used to remember the Head of the linked list.

      1. Node head; // declaration

      2. Node Head = new Node(); // initialization.

      I hope it makes sense.

      Delete
  5. Hi...I saw this code...its awesome...just want to ask what's the use of "temp=tail" in delete operation...

    ReplyDelete
    Replies
    1. You mean tail = temp, it is used to traverse through linked list.

      Delete
    2. mail me at examplesinjava@yahoo.in, still if you have any doubts.

      Delete
  6. Mr. can you create some example of linked list that there is no like . its our project please help me ..

    ReplyDelete
    Replies
    1. Please share your requirement .. what exactly you want to do with Linked list. We will surely try to help you as soon as possible.

      Delete
  7. hello,
    i want tao make sure if this program present stack example or not?
    thanks,

    ReplyDelete
    Replies
    1. This is linked listed program .. you can refer http://examplesinjava.blogspot.in/search/label/Stack for stack example programs.

      Delete
  8. hello sir,,,can this program works using netbeans??

    ReplyDelete
  9. Sir please send me yhe source file .....thanks a lot.....my email is Manoymaryosep@gmail.com

    ReplyDelete
  10. sir please help me in my project.....i have to create ordered list that you can insert,delete and display the status of the list,,,,,,,,,,the program must used is Netbeans IDE............please...........my email is manoymaryosep@gmail.com

    ReplyDelete
  11. In the above program constructor is declared as private member.
    How a private member can be accessed from outside of class object....???

    ReplyDelete
  12. hai...

    I am getting an error as inputmismatchexception after executing the linked list prgm on how to create node, insert elements. the exception is in line 32.
    data = in.nextInt();
    please help me out.

    ReplyDelete
    Replies
    1. You should enter integer value .. for other values it would throw that error.

      Delete
  13. u hve replied ly fr two persons..fr othr prsns u hvent,..if ther s a doubt in d code how to gt rectified??

    ReplyDelete
    Replies
    1. I did now. Please let me know if you anymore queries regarding this.

      Delete
  14. why you have took the condition while(true)

    ReplyDelete
    Replies
    1. instead of condition (true) , any other condition is there to take

      Delete
    2. While true is taken to create infinite number of nodes. This loop will be iterated depends upon the user input.

      Delete
  15. showing error in eclipse node can not be resolve to a type.pls help..

    ReplyDelete
    Replies
    1. please copy and paste the entire error here, so that you get help.

      As per my knowledge there won't be any problem in running this program in eclipse.

      Delete
  16. sir can u plz tell me why we are using tail??
    i didn't understood : tail= head

    ReplyDelete
    Replies
    1. Hi Harshit,

      it is because to remember the starting node the linked list.
      Let say .. first element is 10

      head = 10 and tail = head so tail is also 10 .. then next element will be stored in tail.next ... tail will be moving forward with new nodes .. but not Head . i hope it makes sense.

      Happy learning...... and keep programming let me know if you have any other doubts



      Delete
  17. Their is a problem in code logic if we delete an element from mid of the linkedlist it delete all the next element from that deleted elements

    ReplyDelete
    Replies
    1. Just change the variable 'tail' to something else say 'tail2'

      Delete
  18. How to Replace element in linked list...please give me code

    ReplyDelete
    Replies
    1. if(size>0){
      System.out.println("Enter your element to be replaced");
      data = in.nextInt();
      int newElement = 0;
      System.out.println("Enter the element the replacement");
      newElement = in.nextInt();
      Node newElementNode = new Node(newElement);
      if(head.data==data)
      {
      newElementNode.next = head.next;
      head=newElementNode ;
      }
      else{
      tail=head;
      Node temp=head.next;
      while(temp!=null)
      {
      if(temp.data==data)
      {
      newElementNode.next = temp.next;
      tail=newElementNode;

      break;
      }
      tail=temp;
      temp=temp.next;
      }
      }//else
      }else System.out.println("LinkedList is empty");

      Delete
  19. Size should be decremented even when the matching element is head.

    if(head.data == data){
    head = head.next;
    size--;
    }else {

    ReplyDelete
  20. case :
    System.out.println("Enter your Element at start");
    data = in.nextInt();
    Node newNode1= new Node(data);
    newNode1.next=null;
    if(head ==null){
    head=newNode1;
    tail=head;}
    else{
    newNode1.next=head;// inserting element at start
    head=newNode1;
    }
    size++;
    break;

    another case for inserting element at starting position in linked list

    ReplyDelete
  21. This comment has been removed by a blog administrator.

    ReplyDelete