How to implement three stacks using a single array

后端 未结 17 610
情书的邮戳
情书的邮戳 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

    You can implement three stacks with a linked list:

    • You need a pointer pointing to the next free element. Three more pointers return the last element of each stack (or null, if the stack is empty).
    • When a stack gets another element added, it has to use the first free element and set the free pointer to the next free element (or an overflow error will be raised). Its own pointer has to point to the new element, from there back to the next element in the stack.
    • When a stack gets an element removed it will hand it back into the list of free elements. The own pointer of the stack will be redirected to the next element in the stack.

    A linked list can be implemented within an array.

    How (space) efficent is this?
    It is no problem to build a linked list by using two cells of an array for each list element (value + pointer). Depending on the specification you could even get pointer and value into one array element (e.g. the array is long, value and pointer are only int).
    Compare this to the solution of kgiannakakis ... where you lose up to 50% (only in the worst case). But I think that my solution is a bit cleaner (and maybe more academic, which should be no disadvantage for an interview question ^^).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-23 15:50
    1. Make a HashMap with keys to the begin and end positions e.g. < "B1" , 0 >, <"E1" , n/3 >

    2. for each Push(value) add a condition to check if position of Bx is previous to Ex or there is some other "By" in between. -- lets call it condition (2)

    3. with above condition in mind, if above (2) is true // if B1 and E1 are in order { if ( S1.Push()), then E1 ++ ; else // condition of overflow , { start pushing at end of E2 or E3 (whichever has a space) and update E1 to be E2-- or E3-- ; } }

      if above (2) is false { if ( S1.Push()), then E1 -- ; else // condition of overflow , { start pushing at end of E2 or E3 (whichever has a space) and update E1 to be E2-- or E3-- ; } }

    0 讨论(0)
  • 2020-12-23 15:51

    Split array in any 3 parts (no matter if you'll split it sequentially or interleaved). If one stack grows greater than 1/3 of array you start filling ends of rest two stacks from the end.

    aaa bbb ccc
    1   2   3
    145 2   3
    145 2 6 3
    145 2 6 3 7
    145 286 3 7
    145 286 397
    

    The worse case is when two stacks grows up to 1/3 boundary and then you have 30% of space waste.

    0 讨论(0)
  • 2020-12-23 15:54

    first stack grows at 3n, second stack grows at 3n+1, third grows at 3n+2

    for n={0...N}

    0 讨论(0)
  • 2020-12-23 15:54

    This code implements 3 stacks in single array. It takes care of empty spaces and fills the empty spaces in between the data.

    #include <stdio.h>

    struct stacknode {
       int value;
       int prev;
    };

    struct stacknode stacklist[50];
    int top[3] = {-1, -1, -1};
    int freelist[50];
    int stackindex=0;
    int freeindex=-1;

    void push(int stackno, int value) {
       int index;
       if(freeindex >= 0) {
         index = freelist[freeindex];
         freeindex--;
       } else {
         index = stackindex;
         stackindex++;
       }
       stacklist[index].value = value;
       if(top[stackno-1] != -1) {
         stacklist[index].prev = top[stackno-1];
       } else {
         stacklist[index].prev = -1;
       }
       top[stackno-1] = index;
       printf("%d is pushed in stack %d at %d\n", value, stackno, index);
    }

    int pop(int stackno) {
       int index, value;
       if(top[stackno-1] == -1) {
         printf("No elements in the stack %d\n", value, stackno);
         return -1;
       }
       index = top[stackno-1];
       freeindex++;
       freelist[freeindex] = index;
       value = stacklist[index].value;
       top[stackno-1] = stacklist[index].prev;
       printf("%d is popped put from stack %d at %d\n", value, stackno, index);
       return value;
    }

    int main() {
       push(1,1);
       push(1,2);
       push(3,3);
       push(2,4);
       pop(3);
       pop(3);
       push(3,3);
       push(2,3);
    }

    0 讨论(0)
提交回复
热议问题