0

In 1D stack, I can implement this code below

// Create 1D stack
var stack = new Array(1000)
stack.push(23)
stack.pop()

But for the 2D stack and multi-dimensional stack, how to implement this stack which can do the push and pop function?

Huy
  • 191
  • 6
  • 18

1 Answers1

-1

Here is the example to create 2D stack,

var s = new Array(100);
for( let i=0;i<100;i++) {
  s[i] = [];
  for( let j=0; j<100; j++) {
    s[i][j] = i*j;
  }
}

console.log(s);
Prince Devadoss
  • 416
  • 3
  • 6
  • this is a plain 2D array, what exactly makes it a stack? – Alon Yampolski Jun 05 '19 at 08:28
  • Here this resembles like a stack also by allowing operations like push and pop. – Prince Devadoss Jun 05 '19 at 08:30
  • @AlonYampolski the same thing that makes an array a stack? OP already treats a 1D array as a stack, so it's only logical that multi-dimensional array will also have the same properties as a multi-dimensional stack. – VLAZ Jun 05 '19 at 08:38
  • @VLAZ all i'm saying is that i expect a multi-dimensional stack to be self managed, meaning it will have a functionality like: multiStack.push() and multiStack.pop(). multiStack[n].push() and multiStack[n].pop() is not the same – Alon Yampolski Jun 05 '19 at 08:45
  • @AlonYampolski I'm not aware of this being a hard requirement. In fact, I googled "multidimensional stack" and see people talk about arrays. What you suggests sounds...like a stack. If the interface is just `push` and `pop`, then you don't really care what the implementation underneath is. If you want to be explicit in saying where are you adding and removing elements from, then you really cannot have a self-managing structure. If you don't care, then you don't care what the implementation is underneath. Unless you're making that implementation. – VLAZ Jun 05 '19 at 08:50
  • @VLAZ this is the problem with a not well-defined question :) – Alon Yampolski Jun 05 '19 at 09:25
  • @PrinceDevadoss this is not the answer that i wanted. – Huy Jun 05 '19 at 11:14