使用运行时堆栈实现的输出错误

时间:2018-04-27 04:07:46

标签: java algorithm data-structures

我正在研究这个小应用程序,但遇到了这个问题。我创建了3个类,

1)ARFib

int n; // parameter
int t1, t2; // temp vars
int returnVal;
static int count = 0;

void fib() {
    count++;

    if (n <= 1) {
        returnVal = 1;
     } 
     else {
        ARfib newARfib = new ARfib();
        newARfib.n = n - 1;
        RuntimeStack.push(newARfib);
        newARfib.fib();
        t1 = newARfib.returnVal;
        RuntimeStack.pop();
        returnVal = n + t1;

        newARfib.n = n - 2;
        RuntimeStack.push(newARfib);
        newARfib.fib();
        t1 = newARfib.returnVal;
        RuntimeStack.pop();
        returnVal = t1 + t2;
        //RuntimeStack.display();

    }
}

2)RuntimeStack实施

public class RuntimeStack {

static Nodes top; // this represents the top element of the stack
static int size; // this represents the size of the stack

// to add element to the stack
public static void push(AR newAR) {
    Nodes n = new Nodes(newAR);

    if (isEmpty()) {
        top = n;
        size++;
    } else {

        n.setNext(top);
        top = n;
        size++;
    }

}

// to get data from the stack
public static void pop() {
    if (isEmpty()) {
        System.out.println("Stack is empty");
    } else {
        // AR value = top.getData();
        Nodes temp = top;
        top = top.getNext();
        temp.setNext(null);
        size--;
        // return;
    }

}

// to check if stack is empty
public static boolean isEmpty() {
    if (top == null)
        return true;
    else
        return false;
}

    public static void display() {
    Nodes temp = top;

    while (temp != null) {

        System.out.println(temp.getData());
        System.out.println();
        temp = temp.getNext();
        if (top == null) {
            System.out.println(top);
        }
    }

3)MainFib

public abstract class MainFib {
public static void main(String argv[]) {
    // argv[0]: output file

    Output.setOutput(argv[0]);
    ARfib ex = new ARfib();

     ex.n=10;
    RuntimeStack.push(ex);
    if (ex.n == 0)
        RuntimeStack.display();
    ex.fib();
    System.out.println("The total number of function calls so far = " + ex.count);
    System.out.println();
    RuntimeStack.display();
    System.out.println(ex.toString());

    System.out.println(ex.returnVal);
    RuntimeStack.pop();
    Output.closeOutput();
  }

}

然而,我没有得到所需的输出。以下是所需的输出。

  -----------------------------------------------------------
The total number of function calls so far = 11
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:
ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 15
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 2 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 19
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 3 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 25
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 5 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 29
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 2 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 5 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 35
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 8 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 39
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 2 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 8 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 43
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 3 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 8 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 51
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 13 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 55
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 2 t2 = 0 returnVal = 0``
ARfib n = 5 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 13 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 59
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 3 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 13 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 65
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 5 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 13 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 69
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 2 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 5 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 13 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 77
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 21 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 81
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 2 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 21 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 85
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 3 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 21 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 91
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 5 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 21 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 95
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 2 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 5 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 21 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 101
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 8 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 21 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 105
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 2 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 8 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 21 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 109
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 3 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 8 t2 = 0 returnVal = 0
ARfib n = 9 t1 = 21 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 0 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 119
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 34 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 123
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 2 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 34 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 127
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 3 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 34 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 133
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 5 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 34 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 137
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 2 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 5 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 34 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 143
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 8 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 34 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 147
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 2 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 8 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 34 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 151
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 3 t2 = 0 returnVal = 0
ARfib n = 7 t1 = 8 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 34 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 159
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 13 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 34 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 163
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 2 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 13 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 34 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 167
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 5 t1 = 3 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 13 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 34 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 173
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 3 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 5 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 13 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 34 t2 = 0 returnVal = 0
-----------------------------------------------------------
The total number of function calls so far = 177
The maximum stack size so far = 10

The contents of the runtime stack will be displayed from top to bottom:

ARfib n = 0 t1 = 0 t2 = 0 returnVal = 0
ARfib n = 2 t1 = 1 t2 = 0 returnVal = 0
ARfib n = 4 t1 = 2 t2 = 0 returnVal = 0
ARfib n = 6 t1 = 5 t2 = 0 returnVal = 0
ARfib n = 8 t1 = 13 t2 = 0 returnVal = 0
ARfib n = 10 t1 = 34 t2 = 0 returnVal = 0

这是我的输出

My Output

我不想要代码,但我只是在寻找一些指导或我的代码出错的地方。 ARFib类为我提供了正确的值和函数调用,只是处于混乱的顺序......

1 个答案:

答案 0 :(得分:0)

我会写

public class RuntimeStack {
    //...
    public static void push(AR newAR) {
        Nodes n = new Nodes(newAR);
        if (isEmpty()) {
            top = n;
            size++;
        } else {
            n.setNext(top);
            top = n;
            size++;
        }
        if (newAR.n == 0) 
            display();
    }
    //...
}

对我来说,这已经提供了几乎正确的输出