Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!