向堆叠链表添加问题的方法

时间:2018-12-28 04:07:00

标签: methods types stack

尝试将方法find()添加到堆积的链表中。

我收到此错误:

  

[第91行](第91行是“字符串搜索= stack.find(搜索);”错误:   类型的方法find(java.lang.String)未定义   LinkedStackOfStrings

在Java中运行此代码:

public class LinkedStackOfStings
{

  private Node first;

  private class Node
  {
    private String item;
    private Node next;
  }

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

  //search linked list for "search" string
  public String find(String search)
  {
    String item = first.item;
    first = first.next;
    while (!item.isEmpty())
    {
      if (search.equals(item))
      {
        return "true";
      } 
      item = first.item;
      first = first.next;    
    }
    return "false";
  }

  //add strings to the linked list in the front LIFO
  public void push(String item)
  {
    Node oldFirst = first;
    first = new Node();
    first.item = item;
    first.next = oldFirst;
  }

  //return first item on the list 
  public String pop()
  {
    String item = first.item;
    first = first.next;
    return item;
  }

  public static void main (String[] args)
  {

    String search = (args[0]);
    LinkedStackOfStrings stack = new LinkedStackOfStrings();
    while (!StdIn.isEmpty())
    {
      String item = StdIn.readString();
      stack.push(item);
    }

    String results = stack.find(search);
    if (!results.equals("false"))
    {
      StdOut.println( search + " exists in the linked stack");
    }
    else
    {
      StdOut.println( search + " does not exist in the linked stack");
    }
  }
}

尝试提交您在此上方看到的内容后收到此消息,“看来您的帖子大部分是代码;请添加更多详细信息。”

0 个答案:

没有答案