开发者

Linked List, searching through

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.ne开发者_运维技巧xtNode != 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() );
   }

} 


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜