使用 IntNode Java 更改链表中所有出现的 Int

问题描述 投票:0回答:1

对于我的大学作业,我需要制作一个包含该函数的 IntList

public void replace(int oldValue, int newValue);

函数本身需要能够替换链表中所有出现的整数。该列表是单链表,因此没有以前的功能。这是我的其余代码供理解,非常感谢任何帮助。

import java.util.Scanner;

public class IntListTest {
    private static Scanner scan;
    private static final IntList list = new IntList();
    //----------------------------------------------------------------
// Creates a list, then repeatedly prints the menu and does what
// the user asks until they quit.
//----------------------------------------------------------------
    public static void main(String[] args)
    {
        scan = new Scanner(System.in);
        printMenu();
        int choice = scan.nextInt();
        while (choice != 0)
        {
            dispatch(choice);
            printMenu();
            choice = scan.nextInt();
        }
    }
    //----------------------------------------
// Does what the menu item calls for.
//----------------------------------------
    public static void dispatch(int choice)
    {
        int newVal;
        String info;
        switch (choice) {
            case 0 -> System.out.println("Bye!");
            case 1 -> { //add to front
                System.out.println("Enter integer to add to front");
                newVal = scan.nextInt();
                list.addToFront(newVal);
            }
            case 2 -> { //add to end
                System.out.println("Enter integer to add to end");
                newVal = scan.nextInt();
                list.addToEnd(newVal);
            }         
            case 3 -> {//print
                list.print();
            }
            
            case 4 -> {
                int oldValue, newValue;
                System.out.println("Enter the old value: ");
                oldValue = scan.nextInt();
                System.out.println("Enter the new value: ");
                newValue = scan.nextInt();
                list.replace(oldValue, newValue);
            }
            
            default -> System.out.println("Sorry, invalid choice");
        }
    }
    //-----------------------------------------
    // Prints the user's choices
    //-----------------------------------------
    public static void printMenu()
    {
        System.out.println("\n Menu ");
        System.out.println(" ====");
        System.out.println("0: Quit");
        System.out.println("1: Add an integer to the front of the list");
        System.out.println("2: Add an integer to the end of the list");
        System.out.println("3: Print the list");
        System.out.println("4: Replace all occurrences of a value in the list with a new one.");
        System.out.print("\nEnter your choice: ");
    }
}

public class IntList {
    private IntNode front;

    public IntList(){
        front = null;
    }
    public void addToFront(int val)
    {
        front = new IntNode(val,front);
    }
    //-----------------------------------------
// Adds given integer to end of list.
//-----------------------------------------
    public void addToEnd(int val)
    {
        IntNode new_node = new IntNode(val,null);

        if (front == null) {
            front = new_node;
        }
        else {
            IntNode temp = front;
            while (temp.next != null)
                temp = temp.next;

            temp.next = new_node;
        }
    }
    
    //------------------------------------------------
    // Prints the list elements from first to last.
    //------------------------------------------------
    public void print()
    {
        System.out.println("--------------------");
        System.out.print("List elements: ");
        IntNode temp = front;
        while (temp != null)
        {
            System.out.print(temp.val + " ");
            temp = temp.next;
        }
        System.out.println("\n-----------------------\n");
    }
    //-----------------------------------------
    // Replaces an exact element with a different one
    //-----------------------------------------
    public void replace(int oldValue, int newValue){

    }
}
public class IntNode {
    public int val; //value stored in node
    public IntNode next; //link to next node in list
    //------------------------------------------------------------------
// Constructor; sets up the node given a value and IntNode reference
//------------------------------------------------------------------
    public IntNode(int val, IntNode next)
    {
        this.val = val;
        this.next = next;
    }
}
java list data-structures
1个回答
2
投票

如果您要求替换方法的实现,那么它应该如下所示:

public void replace(int oldValue, int newValue) {
    IntNode temp = front;
    while (temp != null) {
        temp.val = temp.val == oldValue ? newValue : temp.val;
        temp = temp.next;
    }
}

此解决方案与您在

print
方法中编写的解决方案没有什么不同。基本上,您只需要一个辅助节点来迭代列表,而不是打印节点的值,您需要检查当前值是否必须替换为
newValue

从评论中扩展

在此片段中,我使用了三元运算符(或条件运算符)。该语句首先评估问号之前的条件,如果是

true
,则返回冒号之前的表达式 before,否则返回冒号之后的表达式 after。因此,在您的情况下,如果当前节点的值等于
oldValue
,则该节点将更新为
newValue
;否则,将重新分配其当前值。

© www.soinside.com 2019 - 2024. All rights reserved.