search - Linked List, searching through -
my teacher gave practice assignment deals linked lists got code search , searchhelper, having trouble initializing search. how might go that? have tried prompting user variable , throwing through search method error "the method search(t) in type list_3 not applicable arguments (integer)"
the program has to: create linked list, prompt user value search for, use method search recursively searches linked-list object specified value. method should return reference value if it’s found; otherwise, 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 value 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() ); } }
you messing integer character. decide list going be: list<integer> or list<character> (or list<object> or whatever want) not try search , integer in list filled characters ('1', '2', etc.).
'1'.equals(1) => false
good luck!
Comments
Post a Comment