Hi All,
Many people say that implementing LinkedList in Java is not possible. I tried to implement it and thought of sharing so that someone would find it useful.
You can try out the following code. Please feel free to post your ideas/questions w.r.t. this in comments section.
public class LinkedList{
Node start;
int count;
public void add(int data){
Node n=new Node();
n.data=data;
n.next=null;
if(start==null){
start=n;
}
else{
Node temp=new Node();
temp=start;
while(temp.next!=null){
temp=temp.next;
}
temp.next=n;
}
count++;
}
public void display(){
Node n=new Node();
if(start!=null){
n=start;
System.out.println(n.data);
while(n.next!=null){
n=n.next;
System.out.println(n.data);
}
}
}
}
public class LinkedListImpl{
public static void main(String[] args){
LinkedList ll=new LinkedList();
ll.add(10);
ll.add(20);
ll.add(30);
ll.add(40);
ll.display();
}
}
Yes, simple! As simpler as you can ever implement! I just implemented add method and this can be extended because the idea remains the same.
Thanks!
Many people say that implementing LinkedList in Java is not possible. I tried to implement it and thought of sharing so that someone would find it useful.
You can try out the following code. Please feel free to post your ideas/questions w.r.t. this in comments section.
public class LinkedList{
Node start;
int count;
public void add(int data){
Node n=new Node();
n.data=data;
n.next=null;
if(start==null){
start=n;
}
else{
Node temp=new Node();
temp=start;
while(temp.next!=null){
temp=temp.next;
}
temp.next=n;
}
count++;
}
public void display(){
Node n=new Node();
if(start!=null){
n=start;
System.out.println(n.data);
while(n.next!=null){
n=n.next;
System.out.println(n.data);
}
}
}
}
public class LinkedListImpl{
public static void main(String[] args){
LinkedList ll=new LinkedList();
ll.add(10);
ll.add(20);
ll.add(30);
ll.add(40);
ll.display();
}
}
Yes, simple! As simpler as you can ever implement! I just implemented add method and this can be extended because the idea remains the same.
Thanks!