链表,搜索

发布于 2024-10-31 20:13:29 字数 3859 浏览 0 评论 0原文

我的老师给了我们一个处理链接列表的练习作业,我得到了搜索和搜索助手的代码,但我在实际初始化搜索时遇到了麻烦。我该怎么办呢?我尝试提示用户输入一个变量,然后通过搜索方法抛出该变量,但出现错误“List_3 类型中的方法 search(T) 不适用于参数(整数)”

该程序必须:创建一个链接列表,提示用户输入要搜索的值,使用递归搜索链接列表对象以查找指定值的方法搜索。如果找到该值,该方法应返回对该值的引用;否则,它应该返回 null。

 import java.util.Scanner;


class ListNode< T > 
{
   T data; 
   ListNode< T > nextNode;


   ListNode( T object ) 
   { 
      this( object, null ); 
   } 

   ListNode( T object, ListNode< T > node )
   {
      data = object;    
      nextNode = node;  
   } 


   T getData() 
   { 
      return data; 
   } 


   ListNode< T > getNext() 
   { 
      return nextNode;
   } 
} 


public class List_3< T >
{
   private ListNode< T > firstNode;
   private ListNode< T > lastNode;
   private String name; 

   public static void main( String[] args )
   {
      Scanner scan = new Scanner(System.in);
      int result;
      List_3< Character > list1 = new List_3< Character >();
      Integer number;

      list1.insertAtFront( '3' );
      list1.insertAtFront( '4' );
      list1.insertAtBack( '5' );
      list1.insertAtBack( '6' );
      list1.insertAtFront( '2' );
      list1.insertAtFront( '1' );
      list1.insertAtBack( '7' );
      list1.insertAtBack( '8' );
      list1.insertAtFront( '0' );
      list1.insertAtBack( '9' );

      list1.print();
      System.out.println("Please enter a value to search for: ");
      number = scan.nextInt();
      result = search(number);
   }

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


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


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


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


   public T removeFromFront() throws EmptyListException
   {
      if ( isEmpty() ) 
         throw new EmptyListException( name );

      T removedItem = firstNode.data; 


      if ( firstNode == lastNode )
         firstNode = lastNode = null;
      else
         firstNode = firstNode.nextNode;

      return removedItem; 
   }


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

      T removedItem = lastNode.data; 


      if ( firstNode == lastNode )
         firstNode = lastNode = null;
      else 
      { 
         ListNode< T > 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< T > current = firstNode;


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

      System.out.println();
   } 


   public T search( T input )
   {
      return searchHelper( input, firstNode );
   } // end method search


   private T searchHelper( T input, ListNode< T > node )
   {
      if ( node == null )
         return null;
      else if ( node.getData().equals( input ) )
         return node.getData();
      else
         return searchHelper( input, node.getNext() );
   }

} 

My teacher gave us a practice assignment that deals with Linked Lists I got the code for the search and searchhelper, but I am having trouble actually initializing the search. How might I go about that? I have tried prompting the user for a variable and then throwing that through the search method but I get an error "The method search(T) in the type List_3 is not applicable for the arguments (Integer)"

The program has to: create a linked list, prompt the user for a value to search for, use the method search that recursively searches a linked-list object for a specified value. The method should return a reference to the value if it’s found; otherwise, it should return null.

 import java.util.Scanner;


class ListNode< T > 
{
   T data; 
   ListNode< T > nextNode;


   ListNode( T object ) 
   { 
      this( object, null ); 
   } 

   ListNode( T object, ListNode< T > node )
   {
      data = object;    
      nextNode = node;  
   } 


   T getData() 
   { 
      return data; 
   } 


   ListNode< T > getNext() 
   { 
      return nextNode;
   } 
} 


public class List_3< T >
{
   private ListNode< T > firstNode;
   private ListNode< T > lastNode;
   private String name; 

   public static void main( String[] args )
   {
      Scanner scan = new Scanner(System.in);
      int result;
      List_3< Character > list1 = new List_3< Character >();
      Integer number;

      list1.insertAtFront( '3' );
      list1.insertAtFront( '4' );
      list1.insertAtBack( '5' );
      list1.insertAtBack( '6' );
      list1.insertAtFront( '2' );
      list1.insertAtFront( '1' );
      list1.insertAtBack( '7' );
      list1.insertAtBack( '8' );
      list1.insertAtFront( '0' );
      list1.insertAtBack( '9' );

      list1.print();
      System.out.println("Please enter a value to search for: ");
      number = scan.nextInt();
      result = search(number);
   }

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


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


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


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


   public T removeFromFront() throws EmptyListException
   {
      if ( isEmpty() ) 
         throw new EmptyListException( name );

      T removedItem = firstNode.data; 


      if ( firstNode == lastNode )
         firstNode = lastNode = null;
      else
         firstNode = firstNode.nextNode;

      return removedItem; 
   }


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

      T removedItem = lastNode.data; 


      if ( firstNode == lastNode )
         firstNode = lastNode = null;
      else 
      { 
         ListNode< T > 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< T > current = firstNode;


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

      System.out.println();
   } 


   public T search( T input )
   {
      return searchHelper( input, firstNode );
   } // end method search


   private T searchHelper( T input, ListNode< T > node )
   {
      if ( node == null )
         return null;
      else if ( node.getData().equals( input ) )
         return node.getData();
      else
         return searchHelper( input, node.getNext() );
   }

} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你只要分分秒秒 2024-11-07 20:13:29

你把整数和字符搞混了。决定您的列表是什么:ListList(或 List或您想要的任何内容)但不要尝试在填充有字符(“1”、“2”等)的列表中搜索整数。

'1'.equals(1) =>;假的

祝你好运!

You are messing Integer with Character. Decide what your list is going to be: List<Integer> or List<Character> (or List<Object> or whatever you want) but do not try to search and Integer in a list you filled up with Characters ('1', '2', etc.).

'1'.equals(1) => false

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文