简体   繁体   English

两部分,在Java中删除最后一个元素并替换节点中的两个元素

[英]Two part, Removing last element and replacing two elements in node in Java

I'm working a linkedlist from my assignment, one part is to remove the last element from the list, the other part is to replace one element with a new one in the linked list.我正在使用我的任务中的链表,一部分是从列表中删除最后一个元素,另一部分是用链表中的新元素替换一个元素。

I actually struggled to figure out how to put it together, I did some research on removing and replacing, and the result was not really helpful but there was one that is strongly related to replace.我实际上很难弄清楚如何将它放在一起,我做了一些关于移除和替换的研究,结果并没有真正有帮助,但有一个与替换密切相关。

 public boolean replace(int element, int index) {
        Node cursor = first;
        Node prev = null;

        while (cursor != null && index >= 0) {
            index--;
            prev = cursor;
            cursor = cursor.next;
        }

        if (index > 0) return false;

        if (prev != null)
            prev.element = element;

        return true;
    }

but my assignment was specific about not using boolen, but using public void replace(int oldVal, int newVal)但我的任务是关于不使用 boolen,而是使用public void replace(int oldVal, int newVal)

the other part about replacing is like this关于更换的另一部分是这样的

/*********************************
// public void replace(int oldVal, int newVal)
// —replaces all occurrences of oldVal in the list with newVal.
//********************************

    public void replace(int oldVal, int newVal)
    {
          IntNode oldval = new IntNode(oldVal,front);
        IntNode newval = new IntNode(newVal,front);

        if (front == null)
        {
            front = newval;
        }
        else
            while (oldval.next != null)
            {
                oldval = oldval.next;
                oldval.next = newval;
            }

    }

Updated, still struggling if the code is correct to allow me to change one element with a new element in the linked list.已更新,如果代码正确以允许我使用链表中的新元素更改一个元素,则仍在挣扎。

 public IntNode(int val, IntNode next)
       {
           this.val = val;
           this.next = next;
       }

unless I am mistaken and that I dont need to use it.除非我弄错了并且我不需要使用它。

the result shoudl be allowing me to remove a element from the end of the list, and to be able to replace a old value with a new one.结果应该允许我从列表末尾删除一个元素,并能够用新值替换旧值。

Assuming your node's .next is working correctly your removeLast should look something like this:假设您的节点的 .next 正常工作,您的 removeLast 应如下所示:

public void removeLast()
{
    if (front != null && front.next != null)
    {
        IntNode secondtolast = front;
        while (secondtolast.next.next != null)
        {
            secondtolast = secondtolast.next;
        }
        secondtolast.next = null;
    }
}

Based on the description of your replace() function, you are not going to be replacing nodes but rather the values of the nodes.根据您的 replace() 函数的描述,您不会替换节点,而是替换节点的值。

public void replace(int oldVal, int newVal)
{
    while (front.next != null)
    {
        if (front.val == oldVal)
        {
            front.val = newVal;
        }
        front = front.next;
    } 
}

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

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