How to implement 3 stacks with one array?

后端 未结 19 2520
迷失自我
迷失自我 2021-01-29 18:44

Sometimes, I come across the following interview question: How to implement 3 stacks with one array ? Of course, any static allocation is not a solution.

19条回答
  •  没有蜡笔的小新
    2021-01-29 19:15

    For simplicity if not very efficient memory usage, you could[*] divide the array up into list nodes, add them all to a list of free nodes, and then implement your stacks as linked lists, taking nodes from the free list as required. There's nothing special about the number 3 in this approach, though.

    [*] in a low-level language where memory can be used to store pointers, or if the stack elements are of a type such as int that can represent an index into the array.

提交回复
热议问题