gpt4 book ai didi

java - 将节点插入链表

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:40:19 24 4
gpt4 key购买 nike

我是 java 的新手,如果这很糟糕,请提前致歉。基本上我想插入一个数字说链表是 [10 30 50 70] 我想插入 40 所以我输入下一个然后再输入下一个 现在我在 50。我想之前插入但是当我输入before and then 40 它只返回 [10 30 50 70] total = 4 和 current 是 50.

public void insertAfter(long dd)  {   
Node newNode = new Node(dd); // insert a new node after current node;
newNode = current;
current = previous;
if (current != null) { // if current is not null, don't change it; otherwise set current to new node.
return;

}
else {
newNode = current;
}
}

public void insertBefore(long dd) {
Node newNode = new Node(dd); // insert a new node before current node, always set current to the new node
current = previous; // to be implemented
newNode = current;
}

这是调用这两个方法并提供列表的代码。有什么建议吗?

package hw4;

import java.io.*; // for I/O

class TestLinkList {

public static void main(String[] args) throws IOException
{
LinkList theList = new LinkList(); // new list

theList.insertFirst(70);
theList.insertFirst(50);
theList.insertFirst(30);
theList.insertFirst(10);
theList.reset();

while(true) {
System.out.print("Enter first letter of reset, ");
System.out.print("next, get, before, after, delete, exit: ");
System.out.flush();
int choice = getChar(); // get user's option
long value;
switch(choice)
{
case 'r': // reset (to first)
theList.reset();
theList.displayList();
break;
case 'e': // exit the while loop
break;
case 'n': // advance to next item
if( theList.getCurrent() != null ) {
theList.nextLink();
theList.displayList();
} else
System.out.println("Can't go to next link");
break;
case 'g': // get current item
if( theList.getCurrent() != null ) {
value = theList.getCurrent().dData;
System.out.println("Returned " + value);
}
else
System.out.println("List is empty");
break;
case 'b': // insert before current
System.out.print("Enter value to insert: ");
System.out.flush();
value = getInt();
theList.insertBefore(value);
theList.displayList();
break;
case 'a': // insert after current
System.out.print("Enter value to insert: ");
System.out.flush();
value = getInt();
theList.insertAfter(value);
theList.displayList();
break;
case 'd': // delete current item
if( theList.getCurrent() != null ) {
value = theList.deleteCurrent();
System.out.println("Deleted " + value);
theList.displayList();
} else
System.out.println("Can't delete");
break;
default:
System.out.println("Invalid entry");
} // end switch

if (choice == 'e') break;
} // end while

} // end main()

public static String getString() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}

public static char getChar() throws IOException {
String s = getString();
return s.charAt(0);
}

public static int getInt() throws IOException {
String s = getString();
return Integer.parseInt(s);
}

}

最佳答案

因为这是作业,所以我会引导你走向正确的方向。

您的insertAfter 方法是错误的。

第一行你创建了一个新节点。第二行用当前覆盖这个新节点。

从这些错误开始。

最好的方法是用您的链接画一幅画。仔细考虑将新节点放入列表中需要做什么。

关于java - 将节点插入链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3790657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com