链接列表,搜索

时间:2011-04-12 02:46:21

标签: list search recursion

我的老师给了我们一个处理链接列表的练习作业我得到了搜索和搜索帮助的代码,但是我在实际初始化搜索时遇到了麻烦。我怎么能这样做?我试过提示用户输入变量,然后通过搜索方法抛出它,但是我收到错误“List_3类型中的方法搜索(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() );
   }

} 

1 个答案:

答案 0 :(得分:0)

你正在将整数与角色混为一谈。确定您的列表将是什么:List<Integer>List<Character>(或List<Object>或您想要的任何内容)但不要尝试在填充字符的列表中搜索和整数(' 1','2'等。)。

'1'.equals(1)=&gt;假

祝你好运!