For my college work I need to make an IntList that includes the function
public void replace(int oldValue, int newValue);
The function itself needs to be able to replace all occurrences of an integer in the linked list. The list has is a singly linked list therefore has no previous function. Here is the rest of my code for understanding, any help is greatly appreciated.
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;
}
}
CodePudding user response:
If you're asking for an implementation of the replace method, then it should look like this:
public void replace(int oldValue, int newValue) {
IntNode temp = front;
while (temp != null) {
temp.val = temp.val == oldValue ? newValue : temp.val;
temp = temp.next;
}
}
It's not that different from what you've already written in your print
method. You basically use an auxiliary node to iterate your list and instead of printing its value you check whether the value must be replaced with newValue
or not. In case the replacement shouldn't be happening then its current value is simple re-assigned.