How to implement three stacks using a single array

后端 未结 17 801
情书的邮戳
情书的邮戳 2020-12-23 15:10

I came across this problem in an interview website. The problem asks for efficiently implement three stacks in a single array, such that no stack overflows until there is no

17条回答
  •  鱼传尺愫
    2020-12-23 15:48

    Assume you only has integer index. if it's treated using FILO (First In Last Out) and not referencing individual, and only using an array as data. Using it's 6 space as stack reference should help:

    [head-1, last-1, head-2, last-2, head-3, last-3, data, data, ... ,data]

    you can simply using 4 space, because head-1 = 0 and last-3 = array length. If using FIFO (First In First Out) you need to re-indexing.

    nb: I’m working on improving my English.

提交回复
热议问题