简体   繁体   中英

Reverse string method with a linked list

My issue is reversing a linked list that is storing a string. I know my method to reverse the linked list already works, I can use the same method to reverse a list of integers but it doesn't work when reversing a string. It gives me the following error and I'm not exactly sure why.

This is my printInReverse() method

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

Here is how I am calling the method

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

Here is the error message I am receiving

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.

It seems you are trying to call your method on the List object. This interface does not have a printInReverse function so this is an error. just call the function like so:

printInReverse(myList.top);

as opposed to:

myList.printInReverse(myList.top);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM