Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

206
Vistas
What kind of Stack data structure should I use?

I'm writing an algorithm that solves a maze, and I have a maze called char [] [] maze. Its elements be like;

{1,1,1,1,1,1, ..},
{1,0,1,0,1,1, ..},
{1,0,0,1,0,1, ..}, ...

There are 13 Rows and 17 Columns. I have to solve it using the chunk data structure. According to the algorithm I have set up in my mind, I need to store the index values of the navigable path in this stack. For example according to the above maze:

0,0
0,1
0,2
0,3
0,4
1,4
1,5
2,5...

I used to keep an integer number in my previous examples, so I used a structure like this when implementing stack construction.

public class Stack {
    int topOfStack;
    int capacity;
    int[] Stack;

    public Stack(int capacity) {
        this.capacity = capacity;
        Stack = new int[capacity];
        topOfStack = -1;
    }

    void push(int element)
    {
        if(topOfStack == capacity){
            System.out.println("Stack Overflow...");
        }
        else{
            topOfStack++;
            Stack[topOfStack] = element;
        }
    }
}

My question is exactly this. How can I modify this stack structure for my maze solver program? If I need to state it again, I have to keep coordinates or something similar in stack, not integers. Thanks a lot.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can create a new class object which called Coordinates. This class will have two main parameters which are X and Y. Then you can make a stuck that is filled with this Coordinates object instead of a simple Integer.

The stuck you are building can be generic and contain a more complex structures then the basic one and this is what you are looking in this example

over 4 years ago · Santiago Trujillo Denunciar

0

To put it simply, a 2D array could be used to store the coordinates:

public class Stack {
    int topOfStack;
    int capacity;
    int[][] stack;

    public Stack(int capacity) {
        this.capacity = capacity;
        stack = new int[capacity][2];
        topOfStack = -1;
    }

    void push(int x, int y)
    {
        if(topOfStack == capacity){
            System.out.println("Stack Overflow...");
        }
        else{
            stack[++topOfStack] = new int[] { x, y };
        }
    }

    int[] pop() {
        if (topOfStack < 0) {
            System.out.println("Stack is empty");
            return null;
        }
        return stack[topOfStack--];
    }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda