简体   繁体   English

为什么我的双向链接列表会删除以前的链接?

[英]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. 当自己调用addFirst和addLast时,它们都可以工作,但是当我调用addFirst(“ foo”)和addLast(“ bar”)时,最后添加会删除以前添加到列表中的所有内容。 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. 通常,如果您正在执行something.next.next或类似操作,则应该在您的脑海中发出警告。 You'd also be well served to provide a constructor that could take the item instead of the addition statement in the method. 您也将很乐意提供一个可以使用项目而不是方法中的additional语句的构造函数。

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... addFirst而言,因此您不会无意间得到错误的建议,它将像这样...

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 addfirst方法缺少更新指针之一

    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 . 使用Deque接口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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