简体   繁体   中英

Why is my doubly linked list removing previous links?

I Know this topic has been beat to death but I'm really struggling with implementing these two add methods to a linked list. addFirst and addLast both work when called by themselves but when I call addFirst("foo") and addLast("bar") the add last removes anything previously added to the list. add first is supposed to add an item to the beginning of the list, and add last is supposed to append it to the end.

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Deque<Item> implements Iterable<Item> {
private int N; 
private Node first;
private Node last;

//create linked list
private class Node
{
    String item;
    Node next;
    Node previous;
}

public Deque()      // construct an empty deque
{
    N = 2; 
    first = new Node();
    last = new Node();
    //link together first and last node;
    first.next = last;
    last.previous = first; 
    last.item = "Last";
    first.item = "First";


}
public boolean isEmpty()                 // is the deque empty?
{
    return first == null;
}
public int size()                        // return the number of items on the deque
{
    return N;
}
public void addFirst(Item item)          // insert the item at the front
{
    Node nextElement = new Node();
    nextElement.item = (String)item;
    nextElement.next = first.next;
    nextElement.previous = first;
    first.next = nextElement;
    N++;
}
public void addLast(Item item)           // insert the item at the end
{

    Node newLast = new Node();
    newLast.item = (String)item;
    newLast.next = last;
    newLast.previous = last.previous;
    last.previous.next = newLast;
    last.previous = newLast;
    N++;

}

public void printList()
{
    Node print = first;

    for (int i = 0; i < N; i++)
    {

        System.out.print(print.item);
        print = print.next;

    }

    System.out.println("");
}

Seems like you're getting yourself confused. Generally, if your doing something.next.next or similar, a warning should go off in your head. You'd also be well served to provide a constructor that could take the item instead of the addition statement in the method.

public void addLast(Item item)           // insert the item at the end
{
    Node newLast = new Node();
    newLast.item = (String)item;
    if (isEmpty()) {
        first = newLast;
    } else {
        last.next = newLast;
        newLast.previous = last;
    }
    last = newLast;
    N++;
}

As far as addFirst is concerned, so you don't inadvertently get bad advice, it would go something like this...

public void addFirst(Item item) {
    Node newFirst = new Node();
    newFirst.item = (String)item;
    if (isEmpty()) {
        last = newFirst;
    } else {
        first.previous = newFirst;
    }
    newFirst.next = first;
    first = newFirst;
    N++;
}

The addfirst method is missing updating one of the pointers

    public void addFirst(Item item)          // insert the item at the front
{
    Node nextElement = new Node();
    nextElement.item = (String)item;
    nextElement.next = first.next;
    nextElement.previous = first;
    first.next.previous = nextElement; //ADDED HERE
    first.next = nextElement;
    N++;
}

I think this question is answered with one simple link - you're re-inventing the wheel which is always a bad idea, no matter what educational purposes your goals serve.

Use the Deque interface .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM