user4833678
user4833678

Reputation:

Not adding all of the elements into the linkedlist

Why isn't my insertLast(T data) method adding all of the elements into the list?

public class Tester {
    public static void main(String[] args){
        LinkedList<Integer> myList = new LinkedList<Integer>();
        myList.insertLast(1);
        myList.insertLast(2);
        myList.insertLast(3);
        myList.insertLast(4);
        myList.insertLast(5);
        myList.insertLast(6);
        myList.displayList();
    }
}

It adds only 6. What could be the problem with the code?

public class Node<T> {

    public T data;
    public Node<T> next;

    public Node(T data, Node<T> n){
        this.data = data;
        this.next = n;
    }

    public void display(){
        System.out.print(this.data + " ");
    }
}

class LinkedList<T> implements Iterable<T>{

    private Node<T> head;
    private int size;

    public LinkedList(){
        this.head = new Node<T>(null, null);
        this.size = 0;
    }

    public boolean isEmpty(){
        return (head.next == null);
    }

    public void displayList(){
        Node<T> current = head;
        while(current != null){
            current.display();
            current = current.next;
        }
    }

    public void insert(T data){
        head = new Node<T>(data, null);
        size++;
    }

    public void insertLast(T data){
        Node<T> newNode = new Node<T>(data, null);
        if(isEmpty()){
            head = new Node<T>(data, null);
            size++;
        }
        else{
            Node<T> current = head;
            while(current.next != null){
                current = current.next;
            }
            current.next = newNode;
            size++;
        }
    }
}

Upvotes: 1

Views: 43

Answers (3)

Spray
Spray

Reputation: 21

Hai Minor change in your program .You don't have to initialize head.next as null

import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;

public class Node<T> {

    public T data;
    public Node<T> next;

    public Node(T data, Node<T> n){
        this.data = data;
        this.next = n;
    }

    public void display(){
        System.out.print(this.data + " ");
    }
}

class LinkedList<T> implements Iterable<T>{

    private Node<T> head;
    private int size;

    public LinkedList(){
       // this.head = new Node<T>(null, null);
        this.size = 0;
    }

    public boolean isEmpty(){
        return (head == null);
    }

    public void displayList(){
        Node<T> current = head;
        while(current != null){
            current.display();
            current = current.next;
        }
    }

    public void insert(T data){
        head = new Node<T>(data, null);
        size++;
    }

    public void insertLast(T data){
        Node<T> newNode = new Node<T>(data, null);
        if(isEmpty()){
            head = new Node<T>(data, null);
            size++;
        }
        else{
            Node<T> current = head;
            while(current.next != null){
                current = current.next;
            }
            current.next = newNode;
            size++;
        }
    }

    public void forEach(Consumer<? super T> arg0) {
        // TODO Auto-generated method stub

    }

    public Iterator<T> iterator() {
        // TODO Auto-generated method stub
        return null;
    }

    public Spliterator<T> spliterator() {
        // TODO Auto-generated method stub
        return null;
    }
}

Upvotes: 0

Siddhartha
Siddhartha

Reputation: 4444

This check:

if(isEmpty()){
      head = new Node<T>(data, null);
      size++;
  }

initializes head to have a null next, so isEmpty() returns true everytime it's called. You need to check head itself to be null or not in isEmpty(), and in your constructor, instead of:

this.head = new Node<T>(null, null);

you should initialize it to:

this.head = null;

Upvotes: 0

Ben van Gompel
Ben van Gompel

Reputation: 745

Every time you call insertLast, isEmpty returns true, because head.next is null. head.next is only ever set to non-null if isEmpty returns false.

Upvotes: 1

Related Questions