Для работы в колледже мне нужно создать 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
Измените все вхождения Int в связанном списке с помощью IntNode Java. ⇐ JAVA
Программисты JAVA общаются здесь
1732607790
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;
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/72048732/change-all-occurrences-of-an-int-in-a-linked-list-using-intnode-java[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия