【问题标题】:Method to insert and remove Node at the back of a Linked List在链表后面插入和删除节点的方法
【发布时间】:2020-04-14 01:28:14
【问题描述】:

我正在尝试编写一种方法来插入一个节点并在链表的后面删除一个节点。这是我编写方法的主要类。它们位于底部(insertBack 和 removeBack):

公共类LinkedList { 私有链表节点头;

public LinkedList()
{
    head = null;
}

public LinkedListNode getHead()
{
    return head;
}
public void setHead(LinkedListNode h)
{
    head = h;
}

public void insertFront(Object item)
{
    if (head == null)
    {
        LinkedListNode nextNode = new LinkedListNode (item);
        head = nextNode;
    }
    else
    {
        LinkedListNode nextNode = new LinkedListNode (item);
        nextNode.setNext(head);
        head = nextNode;
    }
}

public Object removeFront()
{
    if (head == null)
    {
        return head;
    }
    else
    {
        LinkedListNode t = head;
        Object ret = head.getData();
        head = head.getNext();
        t.setNext(null);
        t = null;
        return ret;
    }

}


//insert a front at the end of linked list
public void insertBack(Object myData)
{
    LinkedListNode newNode = new LinkedListNode(myData);

    if (head == null)
    {
        head = newNode;

    }
    else
    {
        LinkedListNode current = head;
        while (current.getNext() != null)
        {
            current = current.getNext();
        }
        current.setNext(newNode);

    }
}

//remove a node at the end of linked list
public Object removeBack()
{

    if (head == null)
    {
        return null;
    }
    if (head.getNext() == null)
    {
        return null;
    }

    LinkedListNode secondToLast = head;
    while (secondToLast.getNext().getNext() != null)
    {
        secondToLast = secondToLast.getNext();
    }

    secondToLast.setNext(null);
    return head.getData();

}

}

我在此处粘贴此内容时可能存在格式错误,但我仍在尝试弄清楚如何使用此网站。当我运行如下所示的驱动程序类时,我得到的结果如下所示。

public static void main(String[] args)
{
    ValueData data1 = new ValueData("a", 50);
    ValueData data2 = new ValueData("b", 70);
    ValueData data3 = new ValueData("c", 100);

    LinkedList myList = new LinkedList();

    //practice insertBack and removeBack
    myList.insertBack(data1);
    myList.insertBack(data2);
    myList.insertBack(data3);

    System.out.println("Test insertBack and removeBack");

    System.out.println((ValueData) myList.removeBack());
    System.out.println((ValueData) myList.removeBack());
    System.out.println((ValueData) myList.removeBack());
    System.out.println();


    //compare insertFront and removeFront
    System.out.println("Test insertFront and removeFront");
    myList.insertFront(data1);
    myList.insertFront(data2);
    myList.insertFront(data3);

    System.out.println((ValueData)myList.removeFront());
    System.out.println((ValueData)myList.removeFront());
    System.out.println((ValueData)myList.removeFront());
    System.out.println();

RESULTS
Test insertBack and removeBack
a 50.0
a 50.0
null

Test insertFront and removeFront
c 100.0
b 70.0
a 50.0

谁能帮我弄清楚为什么我的 removeFront 和 removeBack 方法不起作用?

【问题讨论】:

  • removeBack() 中,再看看if (head.getNext() == null) { return null; } 和最后的return head.getData();,然后向自己解释为什么你认为这些是正确的。 提示:它们不是。
  • @Andreas 谢谢你的建议!我在学习堆栈和链接列表时遇到了一些麻烦。我知道我的问题可能很愚蠢,但我的大脑实在是太炸了。再次感谢。
  • 我们都搞砸了,别担心。我从经验中学到,很多时候当你的代码不正常并且你请求别人帮助你,然后试图向他们解释你在做什么时,解释的行为会让你有不同的想法,你突然清楚地看到你做错了什么。然后你感谢那个人的帮助,即使他们没有说一句话。 --- 所以当我说“向自己解释为什么你认为这些是正确的”时,我是认真的。解释的行为可能会清除/重定向您的思想,以了解该代码有什么问题。
  • 这里给你另一个提示:返回值是多少?似乎您想返回最后一个元素的值 (data),即从列表中删除的节点。 --- if (head.getNext() == null) { return null; } if 语句检测到列表只有一个元素,即 last 元素。您真的要保持列表不变并返回 null 吗? --- return head.getData(); 你应该返回 last 元素的数据。 head 是最后一个节点吗?

标签: java linked-list stack


【解决方案1】:

首先,这些代码是不必要的,因为没有引用的对象将被丢弃。

t.setNext(null);
t = null;

这也可能有助于 removeBack()

if(head == null) {
   return null;
}

LindedListNode current = head;

while(current.getNext() != null) //searching for last node
   current = current.getNext();

current = null; //make the last node null 

【讨论】:

  • current 设置为null 不会将倒数第二个节点的next 引用设置为null。结果:列表未修改。没有删除任何内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
  • 1970-01-01
  • 2021-07-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-31
  • 1970-01-01
相关资源
最近更新 更多