How to implement 3 stacks with one array?

后端 未结 19 2297
迷失自我
迷失自我 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:23

    I have a solution for this question. The following program makes the best use of the array (in my case, an array of StackNode Objects). Let me know if you guys have any questions about this. [It's pretty late out here, so i didn't bother to document the code - I know, I should :) ]

    public class StackNode {
        int value;
        int prev;
    
        StackNode(int value, int prev) {
            this.value = value;
            this.prev = prev;
        }
    }
    
    
    public class StackMFromArray {
        private StackNode[] stackNodes = null;
        private static int CAPACITY = 10;
        private int freeListTop = 0;
        private int size = 0;
        private int[] stackPointers = { -1, -1, -1 };
    
        StackMFromArray() {
            stackNodes = new StackNode[CAPACITY];
            initFreeList();
        }
    
        private void initFreeList() {
            for (int i = 0; i < CAPACITY; i++) {
                stackNodes[i] = new StackNode(0, i + 1);
            }
        }
    
        public void push(int stackNum, int value) throws Exception {
            int freeIndex;
            int currentStackTop = stackPointers[stackNum - 1];
            freeIndex = getFreeNodeIndex();
            StackNode n = stackNodes[freeIndex];
            n.prev = currentStackTop;
            n.value = value;
            stackPointers[stackNum - 1] = freeIndex;
        }
    
        public StackNode pop(int stackNum) throws Exception {
            int currentStackTop = stackPointers[stackNum - 1];
            if (currentStackTop == -1) {
                throw new Exception("UNDERFLOW");
            }
    
            StackNode temp = stackNodes[currentStackTop];
            stackPointers[stackNum - 1] = temp.prev;
            freeStackNode(currentStackTop);
            return temp;
        }
    
        private int getFreeNodeIndex() throws Exception {
            int temp = freeListTop;
    
            if (size >= CAPACITY)
                throw new Exception("OVERFLOW");
    
            freeListTop = stackNodes[temp].prev;
            size++;
            return temp;
        }
    
        private void freeStackNode(int index) {
            stackNodes[index].prev = freeListTop;
            freeListTop = index;
            size--;
        }
    
        public static void main(String args[]) {
                        // Test Driver
            StackMFromArray mulStack = new StackMFromArray();
            try {
                mulStack.push(1, 11);
                mulStack.push(1, 12);
                mulStack.push(2, 21);
                mulStack.push(3, 31);
                mulStack.push(3, 32);
                mulStack.push(2, 22);
                mulStack.push(1, 13);
                StackNode node = mulStack.pop(1);
                node = mulStack.pop(1);
                System.out.println(node.value);
                mulStack.push(1, 13);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

提交回复
热议问题