Измените все вхождения Int в связанном списке с помощью IntNode Java.JAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Измените все вхождения Int в связанном списке с помощью IntNode Java.

Сообщение Anonymous »

Для работы в колледже мне нужно создать 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;
}
}


Подробнее здесь: https://stackoverflow.com/questions/720 ... tnode-java
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»