0

I'd like to add new nodes into the list without removing/substituting the dummy node head, i.e. head is always null and the list would start from head.next (head -> node -> node -> node). I'm having trouble with the syntax of the dummy node and Im not sure if Im doing it right at all. Could smb please take a look? Thanks in advance!

Im getting a nullPointer in this line of the constructor:

this.head.next = null;

CODE

package SinglyLinkedList;

import java.util.*;

public class Tester {

    public static void main(String[] args){
        LinkedList<Integer> myList = new LinkedList<Integer>();
        myList.insert(1);
        myList.insert(2);
        myList.insert(3);
        myList.displayList();
    }
}

Class Link

package SinglyLinkedList;

import java.util.Iterator;

public class Node<T> {

    public T data;
    public Node<T> next;

    public Node(T data){
        this.data = data;
    }

    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 = null;
        this.head.next = null;
        this.size = 0;
    }

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

    public void displayList(){
        if(head.next == null){
            System.out.println("The list is empty");
        }
        else{
            Node<T> current = head.next;
            while(current != null){
                current.display();
                current = current.next;
            }
        }
    }

    public void insert(T data){
        Node<T> newNode = new Node<T>(data);
        if(head.next == null){
            head.next = newNode;
        }
        else{
            newNode.next = head.next;
            head.next = newNode;
        }
        size++;
    }

    @Override
    public Iterator<T> iterator() {
        // TODO Auto-generated method stub
        return null;
    }
}
mkobit
  • 43,979
  • 12
  • 156
  • 150

1 Answers1

0

I guess you have misunderstood the concept of linked lists. The member variable head points to the start address of the linked list. It cannot be null. head.next should point to the second element while head itself points to the first element. Moreover, you don't have to change the value of head while adding new nodes to the list unless the node you insert is supposed to be placed at the beginning of the linked list. In that case, you need to update head to point to the new node. For inserting nodes in the middle or at the end of the linked list, this is not required.

Further reading:

  1. http://crunchify.com/how-to-implement-a-linkedlist-class-from-scratch-in-java/

  2. http://www.tutorialspoint.com/java/java_linkedlist_class.htm

Swastik Padhi
  • 1,849
  • 15
  • 27
  • But will head still be a dummy node? @CracK –  Oct 22 '15 at 03:14
  • @John Look, according to the basics, head is not even a node. Instead, it is an object reference variable which points to the starting node of a linked list. All it holds is the address of the desired start node. I would recommend reading the concepts properly and trying to implement linked lists from scratch before using the Collections API in Java. – Swastik Padhi Oct 22 '15 at 03:18