gpt4 book ai didi

java - 通过 hasNext() 方法循环递归和反向递归

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

大家好,我是 Java 的新手,所以我非常感谢这方面的任何帮助。 好的,这是我遇到的问题:我有一个列表类和一个 listNode 类,列表类由名称、firstNode 和 lastNode 表示。 firstNode 和 lastNode 来自 listNode 类型,一个 listNode 由一个对象(例如数据或对象 o)和一个 nextNode 表示,它指向列表中的下一个节点,它也来自类型 listNode。

列表类:

public class List {

private ListNode firstNode;
private ListNode lastNode;
private String name;

public List() {
this("list");
}

public List(String listName) {
name = listName;
firstNode = lastNode = null;
}

public void insertAtFront(Object insertItem) {
if (isEmpty())
firstNode = lastNode = new ListNode(insertItem);
else
firstNode = new ListNode(insertItem, firstNode);
}

public void insertAtBack(Object insertItem) {
if (isEmpty())
firstNode = lastNode = new ListNode(insertItem);
else
lastNode = lastNode.nextNode = new ListNode(insertItem);
}

public Object removeFromFront() throws EmptyListException {
if (isEmpty())
throw new EmptyListException(name);
Object removedItem = firstNode.data;

if (firstNode == lastNode)
firstNode = lastNode = null;
else
firstNode = firstNode.nextNode;
return removedItem;
}

public Object removeFromBack() throws EmptyListException {
if (isEmpty())
throw new EmptyListException(name);

Object removedItem = lastNode.data;
if (firstNode == lastNode)
firstNode = lastNode = null;
else {
ListNode current = firstNode;

while (current.nextNode != lastNode)
current = current.nextNode;

lastNode = current;
current.nextNode = null;
}
return removedItem;
}

public boolean isEmpty() {
return firstNode == null;
}

public void print() {
if (isEmpty()) {
System.out.printf("Empty %s\n", name);
return;
}
System.out.printf("The %s is : ", name);
ListNode current = firstNode;

while (current != null) {
System.out.printf("%s", current.data);
current = current.nextNode;
}
System.out.println("\n");
}

@Override
public String toString() {
String stk = "(";
if(isEmpty())return "Empty List";
ListNode checkNode = firstNode;
while (checkNode != null) {
stk += checkNode.data.toString()+ " , ";
checkNode = checkNode.nextNode;
}
return stk+")";
}
public ListNode removeAt (int k){
if(k<=0 || k>getLength())
try{
throw new IllegalValues();
}catch(IllegalValues iv){
iv.printStackTrace();
return null;
}
ListNode newNode = firstNode;
if (k==1) {
ListNode removedNode = firstNode;
firstNode = firstNode.nextNode;
return removedNode;
}
ListNode someNode = firstNode;
for (int i = 1; i < k - 1; i++) {
someNode = someNode.nextNode;
}
ListNode removedNode = someNode.nextNode;
someNode.nextNode = someNode.nextNode.nextNode;
return removedNode;
}
public int getLength(){
ListNode checkNode = firstNode;
int count =0;
while (checkNode != null) {
count++;
checkNode = checkNode.nextNode;
}
return count;
}
public void show(){
if (firstNode==null)
return;
else
System.out.print(firstNode + " ,");
firstNode.show();
}
public void showRev(){
if (lastNode==null)
return;
else
System.out.println(lastNode + ",");
lastNode.showRev();
}
}

列表节点类

public class ListNode {

Object data;
ListNode nextNode;

public ListNode(Object o) {
this(o, null);
}

public ListNode(Object o, ListNode node) {
data = o;
nextNode = node;
}

public Object getObject() {
return data;
}

public ListNode getNext(){
return nextNode;
}

public ListNode show() {
if(this.nextNode == null)return this;
ListNode displayMe = nextNode.show();
System.out.print(displayMe + " , ");
return displayMe;

}

public ListNode showRev() {
if(this.firstNode == null)return this;
ListNode displayMe = lastNode.show();
System.out.print(displayMe + " , ");
return displayMe;

}

}

我有一个名为 show 的递归方法,它从头到尾显示列表中的所有对象,现在我正在尝试制作类似的东西(方法名称是 showRev() ),它显示从头到尾的对象开始(递归方法),我认为不可能有以前的方法所以我有点坚持这个方法。我真的很感激任何想法谢谢大家

最佳答案

如果您的 showRev方法允许带参数,然后我们可以存储每个 ListNodejava.util.List :

public java.util.List<ListNode> showRev(java.util.List<ListNode> nodes) {
if (this.nextNode == null) {
Collections.reverse(nodes);

System.out.println(nodes.stream().collect(Collectors.joining(" ")));

return nodes;
}

nodes.add(lastNode.show());

return showRev(nodes);
}

请注意,这里的递归除了添加 ListNode 外没有做任何特别的事情。 s 到 java.util.List .

要调用此方法,只需将 new ArrayList<>() 传递给它即可.

另外,我会避免使用 List作为类的名称,如 java.util.List很容易与它混淆。

关于java - 通过 hasNext() 方法循环递归和反向递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44828259/

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