我正在為我的資料結構類開發一個專案,該專案要求我撰寫一個類來實作一個整數鏈表。
- 為節點使用內部類。
- 包括以下方法。
- 撰寫一個測驗器,使您能夠以任何順序使用您想要的任何資料測驗所有方法。
我必須創建一個名為“public void insertAt(int index, int item)”的方法。這個方法是為了“在索引位置插入一個專案,索引被傳遞給方法”我在下面有這個方法的代碼。當我執行此方法時,什么也沒有發生。我嘗試添加到特定索引的專案永遠不會被添加。有人知道我做錯了什么嗎?以及如何解決?
import java.util.Random;
import java.util.Scanner;
public class LinkedListOfInts {
Node head;
Node tail;
private class Node {
int value;
Node nextNode;
public Node(int value, Node nextNode) {
this.value = value;
this.nextNode = nextNode;
}
}
public LinkedListOfInts(LinkedListOfInts other) {
Node tail = null;
for (Node n = other.head; n != null; n = n.nextNode) {
if (tail == null)
this.head = tail = new Node(n.value, null);
else {
tail.nextNode = new Node(n.value, null);
tail = tail.nextNode;
}
}
}
public LinkedListOfInts(int[] other) {
Node[] nodes = new Node[other.length];
for (int index = 0; index < other.length; index ) {
nodes[index] = new Node(other[index], null);
if (index > 0) {
nodes[index - 1].nextNode = nodes[index];
}
}
head = nodes[0];
}
public LinkedListOfInts(int N, int low, int high) {
Random random = new Random();
for (int i = 0; i < N; i )
this.addToFront(random.nextInt(high - low) low);
}
public void addToFront(int x) {
head = new Node(x, head);
}
public void insertAt(int index, int item) {
Node temp = head;
Node prev = null;
int i = 0;
for (Node ptr = head; ptr != null; ptr = ptr.nextNode) {
prev = temp;
temp = temp.nextNode;
i ;
}
if (index == i) {
Node newItem = new Node(item, null);
prev.nextNode = newItem;
newItem.nextNode = temp;
}
}
public String toString() {
String result = "";
for (Node ptr = head; ptr != null; ptr = ptr.nextNode) {
if (!result.isEmpty()) {
result = ", ";
}
result = ptr.value;
}
return "[" result "]";
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedListOfInts list = new LinkedListOfInts(10, 1, 20);
boolean done = false;
while (!done) {
System.out.println("1. Insert At");
System.out.println("2. toString");
switch (input.nextInt()) {
case 1:
System.out.println("Insert an Item to a certain Index on the List");
list.insertAt(input.nextInt(), input.nextInt());
break;
case 2:
System.out.println("toString");
System.out.println(list.toString());
break;
}
}
}
}
uj5u.com熱心網友回復:
有一些問題,但您已經解決了大部分問題,您只需將if陳述句移動到if (index == i) {Node newItem... }“for”回圈中,如下所示:
public void insertAt(int index, int item) {
Node temp = head;
Node prev = null;
int i = 0;
for (Node ptr = head; ptr != null; ptr = ptr.nextNode) {
prev = temp;
//Make sure the next node is not null
if (temp.nextNode != null) {
temp = temp.nextNode;
}
//Move the if check here inside the for loop, but before i
if (index == i) {
Node newItem = new Node(item, null);
prev.nextNode = newItem;
//Make sure the next node is not null
if (temp.nextNode != null) {
newItem.nextNode = temp;
}
}
//now advance the index after the above
i ;
}
}
請注意,您的代碼有一個錯誤,該錯誤已通過檢查下一個節點不為空來修復。
我們可以通過在索引 2 后插入 999 來看到更新的方法有效:
1. Insert At 2. toString 1 Insert an Item to a certain Index on the List 2 999 1. Insert At 2. toString 2 toString [5, 18, 8, 999, 11, 11, 1, 19, 3, 1, 10] 1. Insert At 2. toString
如果您希望將專案插入索引 2,則調整順序并將if陳述句放在之前prev = temp;
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/323922.html
上一篇:如何從具有值串列的字典串列中形成DataFrame?
下一篇:如何將專案添加到鏈接串列的末尾?
