Reputation: 27
I am trying to implement linked list in java. In my main class I get some integers from user and put them in a linked list and then print out my linked list elements. Everything's working fine so far, however I think in my main class, it makes sense to first print out each element's data and then move on to the next element. When I do that, it doesn't print the last element of my list but it prints the first element twice. I decided to first move on to the next element and then print the previous element's data and it works just fine!!! Can anyone explains why?(check out the last two lines of my code).
public class Node {
Node next;
int data;
public Node(int data){
this.data=data;
}
}
My linked list class:
public class LinkedList {
Node head;
public void append(int data){
if(head==null){
head=new Node(data);
}
Node current;
current=head;
while(current.next!=null){
current=current.next;
}
current.next=new Node(data);
}
}
My main class:
public class Main {
static LinkedList linkedList =new LinkedList();
public static void main(String [] args){
System.out.println("please enter numbers you wanna store in a linked list");
Scanner scanner=new Scanner(System.in);
while (scanner.hasNextInt()){
linkedList.append(scanner.nextInt());
}
if (linkedList.head!=null){
Node current;
current=linkedList.head;
while (current.next!=null){
**current=current.next;
System.out.println(current.data);**
}
}
}
}
Upvotes: 0
Views: 12122
Reputation: 2119
Interchange these 2 statements. Print the data before going on to next node:
System.out.println(current.data);
current=current.next;
and change the while condition from current.next!=null
to while current!=null
because current.next() is going to be null for the last Node and hence won't be printed
Also you are adding the 1st element twice in your append
method. Change it to below:
public void append(int data){
if(head==null){
head=new Node(data);
}
else{
Node current;
current=head;
while(current.next!=null){
current=current.next;
}
current.next=new Node(data);}
}
Upvotes: 3
Reputation: 79838
You've got two bugs.
(1) You're adding the first element twice. You've got
if(head==null){
head=new Node(data);
}
but then you continue on and add it again.
(2) When you print out the list, you're stopping when current.next == null
- so you're stopping before you get to the last element. The condition on your while
loop needs to be
while(current != null) {
instead of checking current.next
.
Upvotes: 3
Reputation: 34628
In your append
statement, you add the first element twice:
public void append(int data){
if(head==null){
head=new Node(data); // <--- Added here
}
Node current;
current=head;
while(current.next!=null){
current=current.next;
}
current.next=new Node(data); // <--- And then again here
}
So your linked list actually contains the first element twice. It doesn't happen afterwards, because there is already a head to the list.
You should add a return
after the head = new Node(data)
, or have an else statement:
public void append(int data){
if(head==null){
head=new Node(data);
} else {
Node current;
current=head;
while(current.next!=null){
current=current.next;
}
current.next=new Node(data);
}
}
Upvotes: 3