Java remove an element from linked list recursion -
for assignment given me, supposed move through linked list of size 1,000,000 iteratively , recursively. have iterative part down confused on recursive part. have concept down have told teacher cannot recursively because inevitably stack overflow error. says should not getting problem totally stuck. tips great have no idea doing wrong.
public static void main(string[] args) { system.out.println("please enter how many random numbers want generate: "); scanner prompt = new scanner(system.in); int amount = prompt.nextint(); random rnd = new random(); system.out.println("creating list..."); (int = 0; < amount; i++) { randomlist.add(rnd.nextint(100)); } system.out.println("going through list..."); iteratenumbers(); long starttimerec = system.nanotime(); recursivenumbers(randomlist); long elapsedtimerec = system.nanotime() - starttimerec; double seconds = (double)elapsedtimerec / 1000000000.0; system.out.println("recursively, function took " + seconds + " seconds."); prompt.close(); } //create linked list public static linkedlist<integer> randomlist = new linkedlist<integer>(); private static void iteratenumbers() { long starttime = system.nanotime(); (int = 0; < randomlist.size(); i++) { randomlist.remove(); } long elapsedtime = system.nanotime() - starttime; double seconds = (double)elapsedtime / 1000000000.0; system.out.println("iteratively, function took " + seconds + " seconds."); } private static void recursivenumbers(linkedlist<integer> current) { if (current.isempty() == true) { return; } else { current.removefirst(); recursivenumbers(current); } } }
it's not hard understand. when run program, stack area not enough,so have error.
you can try add space of stack.
run program following arguments:
-xss1280m
it means create stack 1280mb, , see result this:

Comments
Post a Comment