Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 链接列表,搜索_List_Search_Recursion - Fatal编程技术网

List 链接列表,搜索

List 链接列表,搜索,list,search,recursion,List,Search,Recursion,我的老师给了我们一个关于链表的练习作业,我得到了搜索和searchhelper的代码,但是我在初始化搜索时遇到了问题。我该怎么做呢?我试图提示用户输入一个变量,然后通过搜索方法抛出该变量,但我得到一个错误“类型列表_3中的方法搜索(T)不适用于参数(整数)” 程序必须:创建一个链表,提示用户输入要搜索的值,使用search方法递归搜索链表对象中的指定值。如果找到该值,则该方法应返回对该值的引用;否则,它应该返回null import java.util.Scanner; class Lis

我的老师给了我们一个关于链表的练习作业,我得到了搜索和searchhelper的代码,但是我在初始化搜索时遇到了问题。我该怎么做呢?我试图提示用户输入一个变量,然后通过搜索方法抛出该变量,但我得到一个错误“类型列表_3中的方法搜索(T)不适用于参数(整数)”

程序必须:创建一个链表,提示用户输入要搜索的值,使用search方法递归搜索链表对象中的指定值。如果找到该值,则该方法应返回对该值的引用;否则,它应该返回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() );
   }

} 
import java.util.Scanner;
类ListNode
{
T数据;
ListNodenextNode;
ListNode(T对象)
{ 
这个(object,null);
} 
ListNode(T对象,ListNodenode)
{
数据=对象;
nextNode=节点;
} 
T getData()
{ 
返回数据;
} 
ListNodegetNext()
{ 
返回下一个节点;
} 
} 
公共类列表_3
{
私有ListNodefirstNode;
私有ListNodelastNode;
私有字符串名称;
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
int结果;
列表3list1=新列表3();
整数;
列表1.插入前('3');
列表1.插入前('4');
列表1.InsertBack('5');
列表1.InsertBack('6');
列表1.插入前('2');
列表1.插入前('1');
列表1.InsertBack('7');
列表1.InsertBack('8');
列表1.InsertAfront('0');
列表1.InsertBack('9');
list1.print();
System.out.println(“请输入要搜索的值:”);
number=scan.nextInt();
结果=搜索(编号);
}
公开名单(三)
{ 
本(“名单”);
} 
公共列表_3(字符串列表名)
{
名称=列表名称;
firstNode=lastNode=null;
} 
公共空白插入项前面(T插入项)
{
if(isEmpty())
firstNode=lastNode=newlistnode(插入项);
其他的
firstNode=新的ListNode(插入项,firstNode);
} 
public void insertback(T insertItem)
{
if(isEmpty())
firstNode=lastNode=newlistnode(插入项);
其他的
lastNode=lastNode.nextNode=newlistnode(插入项);
} 
public T removeFromFront()抛出EmptyListException
{
if(isEmpty())
抛出新的EmptyListException(名称);
T removedItem=firstNode.data;
if(firstNode==lastNode)
firstNode=lastNode=null;
其他的
firstNode=firstNode.nextNode;
返回移除的项目;
}
public T removeFromBack()抛出EmptyListException
{
if(isEmpty())
抛出新的EmptyListException(名称);
T removedItem=lastNode.data;
if(firstNode==lastNode)
firstNode=lastNode=null;
其他的
{ 
ListNodecurrent=firstNode;
while(current.nextNode!=lastNode)
current=current.nextNode;
lastNode=当前;
current.nextNode=null;
} 
返回移除的项目;
} 
公共布尔值为空()
{ 
返回firstNode==null;
}
公开作废印刷品()
{
if(isEmpty())
{
System.out.printf(“空的%s\n”,名称);
回来
} 
System.out.printf(“该%s为:”,名称);
ListNodecurrent=firstNode;
while(当前!=null)
{
System.out.printf(“%s”,当前数据);
current=current.nextNode;
} 
System.out.println();
} 
公共T搜索(T输入)
{
返回searchHelper(输入,第一个节点);
}//结束方法搜索
专用T searchHelper(T输入,ListNodenode)
{
if(node==null)
返回null;
else if(node.getData().equals(输入))
返回node.getData();
其他的
返回searchHelper(输入,node.getNext());
}
} 

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

“1”。等于(1)=>false


祝你好运

+1表示不要忘记标记为家庭作业=)search和searchhelper的代码在哪里?它们是print方法下的最后两个方法。您的列表使用“Character”类型而不是integer。感谢您指出该部分,但我的问题仍然存在,我的搜索参数在尝试让它搜索时仍然是一个麻烦。Eclipse一直试图使方法搜索保持静态,这会把一切搞得一团糟。它还试图将T强制转换为对象,但强制转换T会给我一个错误(无法对非静态类型T进行静态引用)