Mustafa Abdul-Kader Mustafa Abdul-Kader - 5 months ago 21
Java Question

Singly-linked List Insertion Before Index

I have two classes, DataStructure as well as LinkedList. My DataStructure class is responsible for insertion / deletion / replacement / update / display of the Linked List. I'm having issues with inserting an element inside the linked list before the indexed element. When I debug the code and look at the LinkedList class, it's evident that the link breaks and is not continuous. I'm not sure what is wrong in the code as it all appears logically sound. If someone could look at this and guide me in the right direction, I'd greatly appreciate it.

This is only a snippet of the whole project, so feel free to change the type

DataStructure class:

public class DataStructure {
//Item LinkedList field
private LinkedList<Item> itemLinkedList;

//Constructor
public DataStructure() {
itemLinkedList = new LinkedList<>();
}

//Add Item
public boolean addItem(Item item) {
boolean returnVal = false;
if (item != null) {
itemLinkedList.Move(2);
itemLinkedList.Add(item);
returnVal = true;
}
return returnVal;
}
}


LinkedList class

public class LinkedList<T> {
//Fields
private Node<T> Head;
private Node<T> Current;
private Node<T> Tail;
private int size;

//Constructor
public LinkedList() {

}

//Methods
private void AddHead(T data) {
Node<T> temp = new Node<>(data);
Tail = Current = Head = temp;
}

public void Add(T data) {
if (data == null) throw new NullPointerException();
else {
Node<T> temp = new Node<>(data);
if (Head == null) AddHead(data);
else {
if (Current == Tail) {
Tail.setNext(temp);
Current = Tail = temp;
} else if (Current == Head) {
temp.setNext(Head);
Current = Head = temp;
} else {
Node<T> cu = Current;
Current = temp;
Current.setNext(cu);
}
}
size++;
}
}

public Node MoveNext() {
if (Current.getNext() != null)
return Current = Current.Next;
else return Current;
}

public void MoveLast() {
if (Tail != null)
Current = Tail;
}

public void MoveFirst() {
if (Head != null)
Current = Head;

}

public void Move(int index) {
if (index >= size) MoveLast();
else {
MoveFirst();
for (int i = 0; i < index; i++) MoveNext();
}
}

class Node<T> {
private T data;
private Node<T> Next;

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

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

public T getData() {
return data;
}

public void setData(T data) {
this.data = data;
}

public Node<T> getNext() {
return Next;
}

public void setNext(Node<T> next) {
Next = next;
}

public boolean hasNext() {
return Next != null;
}
}
}

Answer

What I am observing that there is a problem in Add() method in LinkedList class. Below is the modified version: Please try it once. One more thing, in the Move(int index) method the loop should run for (index -1) times, otherwise you will not be able to link the previous link.

So please try once below modified methods and tell me if problem is still not resolved:

  public void Move(int index) {
        if (index >= size) MoveLast();
        else {
            MoveFirst();
            for (int i = 0; i < index-1; i++) MoveNext();  //Run loop until (index - 1) so that you can have reference at the node no. 2 (in case of index 2)
        }
    }

 public void Add(T data) {
        if (data == null) throw new NullPointerException();
        else {
            Node<T> temp = new Node<>(data);
            if (Head == null) AddHead(data);
            else {
                if (Current == Tail) {
                    Tail.setNext(temp);
                    Current = Tail = temp;
                } else if (Current == Head) {
                    temp.setNext(Head);
                    Current = Head = temp;
                } else {
                    Node<T> cu = Current.getNext();
                    Current.setNext(temp);
                    temp.setNext(cu);
                    Current = cu; //Recently added node
                }
            }
            size++;
        }
    }