I have a Depth Limited Search algorithm that I run several times in a loop so that the algorithm can act like an Iterative Deepening Search algorithm, Why does the code return the desired result only when I call the method explicitly but not when I call it in the loop.
public class DepthLimited {
public static void main(String[] args) {
    Graph graph = new Graph();
    graph.addNode('A');    // Since A is the first vertex so it takes the index 0  
    graph.addNode('E');    // B is the second one so it takes index 1
    graph.addNode('B');    // index 2
    graph.addNode('C');    // index 3
    graph.addNode('D');    // index 4
    graph.addEdge(0, 1);     // This indicates the relation between the vertices A and E
    graph.addEdge(1, 2);     // This indicates the relation between the vertices E and B
    graph.addEdge(0, 3);     // This indicates the relation between the vertices A and C
    graph.addEdge(3, 4);     // This indicates the relation between the vertices C and D
 //        System.out.print("Sequence: ");
 //        graph.dls(2);
 //        System.out.println("");
    graph.dls(2); // produces AEBCD when called like this, before any other similar calls i.e graph.dls(1)
    for (int i = 0; i <= 2; i++) {
        graph.dls(i);  // when i is 2 the method only returns A instead of AEBCD
        System.out.println("");
    }
}
}
And here's my Graph class:
public class Graph {
private final int MAX_NODES = 20;      //maximum number of nodes
private Node[] listOfNodes;        // list of nodes
private int neighbourMatrix[][];      // every node with its children
private int noOfNodes;              // current number of nodes
private Stack stack;
private int depth = 0;
public Graph() {
    listOfNodes = new Node[MAX_NODES];
    neighbourMatrix = new int[MAX_NODES][MAX_NODES];
    stack = new Stack();
}
public void addNode(char name) {
    listOfNodes[noOfNodes++] = new Node(name);        //create a new node and add it to the array of nodes
}
public void addEdge(int start, int end) {                //creates a bidirectional relation between
    neighbourMatrix[start][end] = 1;                     //the two nodes (start and end)
    neighbourMatrix[end][start] = 1;                     // 1 is used to indicate the existence of a relation between
}                                                        //two node because by default neighbourMatrix contains only 0s.
public void display(int node) {
    System.out.print(listOfNodes[node].name);          //prints the name of a node
}
public void dls(int limit) {                                 // begin at node 0 which is the root of the tree
    listOfNodes[0].checked = true;  // mark it
    display(0);                 // display it
    stack.push(0);                 // push it to the stack
    depth++;
    while (!stack.isEmpty()) // until stack empty,
    {
        int node = getUnvisitedChild(stack.peek());
        if (depth <= limit) {
            // get an unvisited child of the node that is at the top of the stack
            if (node == -1) // if the node had no unvisited child, then pop the node from the stack
            {
                stack.pop();
                depth--;
            } else // if the node has unvisited child         
            {
                listOfNodes[node].checked = true;  // mark it
                display(node);                 // display it
                stack.push(node);                 // push it to the stack
                depth++;
            }
        } else {
            stack.pop();
            depth--;
        }
    }
}
public int getUnvisitedChild(int v) // returns an unvisited child of the node v 
{
    for (int j = 0; j < noOfNodes; j++) {
        if (neighbourMatrix[v][j] == 1 && listOfNodes[j].checked == false) {
            return j;               //returns the index of the child
        }
    }
    return -1;              //otherwise it returns -1
}
}
 
    