/**
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
}
}
}
36 Comments
Please give a option to download the code :) . Fantastic work.
ReplyDeleteGot the code. Had some trouble in copying. Sorry for the last comment.
ReplyDeleteshare your email id .. still you need the source file
DeleteThis comment has been removed by the author.
ReplyDelete@Anamica: Yes, both are instances of same class type. Node Head used to remember the Head of the linked list.
Delete1. Node head; // declaration
2. Node Head = new Node(); // initialization.
I hope it makes sense.
Hey Hi have a question...I am confused between 2 statements.....
ReplyDelete1)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..
@Anamica: Yes, both are instances of same class type. Node Head used to remember the Head of the linked list.
Delete1. Node head; // declaration
2. Node Head = new Node(); // initialization.
I hope it makes sense.
Hi...I saw this code...its awesome...just want to ask what's the use of "temp=tail" in delete operation...
ReplyDeleteYou mean tail = temp, it is used to traverse through linked list.
Deletemail me at examplesinjava@yahoo.in, still if you have any doubts.
DeleteMr. can you create some example of linked list that there is no like . its our project please help me ..
ReplyDeletePlease share your requirement .. what exactly you want to do with Linked list. We will surely try to help you as soon as possible.
Deletehello,
ReplyDeletei want tao make sure if this program present stack example or not?
thanks,
This is linked listed program .. you can refer http://examplesinjava.blogspot.in/search/label/Stack for stack example programs.
Deletehello sir,,,can this program works using netbeans??
ReplyDeleteSir please send me yhe source file .....thanks a lot.....my email is Manoymaryosep@gmail.com
ReplyDeletesir 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
ReplyDeleteIn the above program constructor is declared as private member.
ReplyDeleteHow a private member can be accessed from outside of class object....???
hai...
ReplyDeleteI 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.
You should enter integer value .. for other values it would throw that error.
Deleteu hve replied ly fr two persons..fr othr prsns u hvent,..if ther s a doubt in d code how to gt rectified??
ReplyDeleteI did now. Please let me know if you anymore queries regarding this.
Deletewhy you have took the condition while(true)
ReplyDeleteinstead of condition (true) , any other condition is there to take
DeleteWhile true is taken to create infinite number of nodes. This loop will be iterated depends upon the user input.
Deleteshowing error in eclipse node can not be resolve to a type.pls help..
ReplyDeleteplease copy and paste the entire error here, so that you get help.
DeleteAs per my knowledge there won't be any problem in running this program in eclipse.
sir can u plz tell me why we are using tail??
ReplyDeletei didn't understood : tail= head
Hi Harshit,
Deleteit 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
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
ReplyDeleteJust change the variable 'tail' to something else say 'tail2'
DeleteHow to Replace element in linked list...please give me code
ReplyDeleteif(size>0){
DeleteSystem.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");
Size should be decremented even when the matching element is head.
ReplyDeleteif(head.data == data){
head = head.next;
size--;
}else {
case :
ReplyDeleteSystem.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
This comment has been removed by a blog administrator.
ReplyDelete