Exemple #1
0
        public int Pop(int stackNum)
        {
            StackData stack = stacks[stackNum];

            if (stack.size == 0)
            {
                throw new Exception("Trying to pop an empty stack.");
            }
            int value = buffer[stack.pointer];

            buffer[stack.pointer] = 0;
            stack.pointer         = PreviousElement(stack.pointer);
            stack.size--;
            return(value);
        }
Exemple #2
0
        public void Push(int stackNum, int value)
        {
            StackData stack = stacks[stackNum];

            /* Check that we have space */
            if (stack.size >= stack.capacity)
            {
                if (NumberOfElements() >= total_size)
                { // Totally full
                    throw new Exception("Out of space.");
                }
                else
                { // just need to shift things around
                    Expand(stackNum);
                }
            }

            /* Find the index of the top element in the array + 1,
             * /* and increment the stack pointer */
            stack.size++;
            stack.pointer         = NextElement(stack.pointer);
            buffer[stack.pointer] = value;
        }
Exemple #3
0
        private void Shift(int stackNum)
        {
            StackData stack = stacks[stackNum];

            // increase the capacity
            if (stack.size >= stack.capacity)
            {
                int nextStack = (stackNum + 1) % number_of_stacks;
                Shift(nextStack); // make some room
                stack.capacity++;
            }

            // Shift elements in reverse order

            for (int i = (stack.start + stack.capacity - 1) % total_size; stack.IsWithinStack(i, total_size); i = PreviousElement(i))
            {
                buffer[i] = buffer[PreviousElement(i)];
            }

            buffer[stack.start] = 0;
            stack.start         = NextElement(stack.start);   // move stack start
            stack.pointer       = NextElement(stack.pointer); // move pointer
            stack.capacity--;                                 // return capacity to original
        }
Exemple #4
0
        public bool IsEmpty(int stackNum)
        {
            StackData stack = stacks[stackNum];

            return(stack.size == 0);
        }
Exemple #5
0
        public int Peek(int stackNum)
        {
            StackData stack = stacks[stackNum];

            return(buffer[stack.pointer]);
        }