1

I am really struggling in adding a node at the end of a double linked list, adding to the head was easy, now I really can't find a way to add to the end. It seems stupid but I'm losing a lot of time, so if anyone can help that would be appreciated. Here's my partial implementation in Java.

public class DoublyLinkedList implements Iterable<Node>, Iterator<Node> {

private Node head = new Node();
private Node tail = head;
private Node current;
private Node previous;

@Override
public Iterator<Node> iterator() {
    current = head;
    previous = null;
    return this;
}

public void addToHead(Node node) {
    Node next = head.getNext(null);
    Node previous = head.getNext(next);
    head.setNext(previous, node);
    node.setNext(null, head);
    head = node;
}

@Override
public boolean hasNext() {
    return current != tail;
}

@Override
public Node next() {
    Node tmp = previous;
    previous = current;
    current = current.getNext(tmp);
    return previous;
}

And here's Node Class

public class Node {
Node next = null;
Node previous = null;


Node getNext(Node node){
    if(node == next){
        return previous;
    }

    if(node == previous){
        return next;
    }

    throw new IllegalStateException("something went wrong");
}

void setNext(Node node, Node next){
    if(node == previous){
        previous = next;
    }
    else if(node == this.next){
        this.next = next;
    }
    else{
        throw new IllegalStateException("something went wrong");
    }
}


}

I hope you will understand the code above, basically I will need the following :

public void addToEnd(Node node) {
    // implementation
}
geek4079
  • 539
  • 2
  • 6
  • 15

2 Answers2

1

What interface are you aiming to implement on this double-linked list? The way you've implemented getNext and setNext is not a clear way of doing it. If you only have one node, and you attempt to setNext with a null value for node, there is no way of telling unless you look at the code where your new node is going to end up. Along with that, I'm not sure if there is a nice way for you to set the previous node of a new tail node with the way you've implemented it. It's very ambiguous.

Can I recommend that you instead implement getNext(), setNext(Node), getPrevious() and setPrevious(Node) methods in your Node class? This would greatly simplify and clear up your code.

You would then be able to implement your addToEnd(Node) method very simply.

public void addToEnd(Node node) {
    node.setPrevious(tail);
    tail.setNext(node);
    tail = node;
}

If you want to be able to iterate through the list in both directions using 'getNext()' and 'hasNext()', you could provide a forward or reverse Iterator implementation. There are examples of how to create Iterators floating around. The first answer on this question has an example of a reverse iterator.

Community
  • 1
  • 1
AndyN
  • 2,075
  • 16
  • 25
0

The basic idea is just the same as adding at the beginning of the list.

We can split it up into two parts:

  1. adding the item to the list
  2. updating the head/tail-node of the list

In pseudocode:

node to_insert

//step 1
tail.setNext(to_insert)
to_insert.setPrevious(tail)

//step 2
tail = to_insert