簡體   English   中英

帶鏈表的反向字符串方法

[英]Reverse string method with a linked list

我的問題是反轉存儲字符串的鏈表。 我知道我的反轉鏈接列表的方法已經有效,我可以使用相同的方法反轉整數列表,但是在反轉字符串時它不起作用。 它給了我以下錯誤,我不確定為什么。

這是我的 printInReverse() 方法

public void printInReverse(Node L)
{
  if (L.next == null)
  {
     System.out.print(L.data + " ");
  }
  else
  {
     printInReverse(L.next);
     System.out.print(L.data + " ");
   }
}

這是我調用該方法的方式

System.out.print("Reversed string: "); myList.printInReverse(myList.top);

這是我收到的錯誤消息

ReverseString_Scott_Robinson.java:24: error: cannot find symbol
System.out.print("Reversed string: "); myList.printInReverse(myList.end);
                                             ^ 
symbol:   method 
printInReverse(Stack_Scott_Robinson<String>.Node<String>)
location: variable myList of type Stack_Scott_Robinson<String>
Note: ReverseString_Scott_Robinson.java uses unchecked or unsafe operations.

您似乎正在嘗試在列表 object 上調用您的方法。 這個接口沒有 printInReverse function 所以這是一個錯誤。 只需像這樣調用 function :

printInReverse(myList.top);

相對於:

myList.printInReverse(myList.top);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM