3

For this assignment given to me, I am supposed to move through a linked list of size 1,000,000 iteratively and recursively. I have the iterative part down but I am confused on the recursive part. I have the concept down but I have told my teacher that I cannot recursively do it because I will inevitably get a stack overflow error. He says I should not be getting that problem but I am totally stuck. Any tips would be great as I have no idea what I am doing wrong.

public static void main(String[] args) {

    System.out.println("Please enter how many random numbers you want to generate: ");
    Scanner prompt = new Scanner(System.in);
    int amount = prompt.nextInt();

    Random rnd = new Random();
    System.out.println("Creating list...");
    for (int i = 0; i < 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, the function took " + seconds + " seconds.");

    prompt.close();

}
//create the linked list
public static LinkedList<Integer> randomList = new LinkedList<Integer>();

private static void iterateNumbers() {

    long startTime = System.nanoTime();
    for (int i = 0; i < randomList.size(); i++) {
        randomList.remove();
    }
    long elapsedTime = System.nanoTime() - startTime;
    double seconds = (double)elapsedTime / 1000000000.0;
    System.out.println("Iteratively, the function took " + seconds + " seconds.");
}

private static void recursiveNumbers(LinkedList<Integer> current) {     

    if (current.isEmpty() == true) {
        return;
    } else {
        current.removeFirst();
        recursiveNumbers(current);
    }
}

}
1
  • What did you get with this code? What is your exact question? Commented Mar 25, 2014 at 1:50

2 Answers 2

1

It's not very hard to understand. When you run your program, the stack area is not enough,so you have a error.

You can try to add the space of the stack.

run the program with the following arguments:

-Xss1280m

It means create the stack with 1280MB, and you will see the result like this:


enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

how would i go about running my program with those arguments? is it possible to make that statement within my code so it would automatically allocate a greater stack area for anyone who runs the program?
You can compile the java file first with the code javac Main.java, and run with the code:java -Xss1280m Main.And I write the shell script to run the program instead of run it directly.Here is my script:java -Xss1280m Main,and I just run with sh run.sh,and it works.If you are in windows, you can also write the .bat file.
okay I gotcha, but is there any way to write in my .java file to create a stack with 1280MB?
I am not sure whether there is any way we can do that.But what I see is a script with a config file until now.
@Arkant0r: I think it is not possible to specify the stack size from the java source code because it would be a security risk.
|
1

In the iterative version , you should pass in the index at the remove.

randomList.remove(i);

And should you not also repopulate the linked list before you call the recursive method?

    for (int i = 0; i < amount; i++) {
    randomList.add(rnd.nextInt(100));
}

Not too sure about the Java implementation of the Linked List. Tried looking it up there. Is there a isEmpty() method? Or should you try:

if(current.size() < 1) 
    return;

2 Comments

you're right I forgot to repopulate the list! Let me get back to you once i try that.
Well I repopulated the list and passed the index at the remove statement but I'm still getting a stackoverflowerror when I create a linked list with 1,000,000 integers and then use the recursive function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.