Пример #1
0
            public int Pop(int stackIndex)
            {
                SingleStack s = stacks[stackIndex];

                return(s.Pop());
                //Need to implement stack size shrinking here.
            }
Пример #2
0
            public void Push(int num, int stackIndex)
            {
                SingleStack s = stacks[stackIndex];

                if (s.Push(num))
                {
                    return;
                }
                int totalSize = GetTotalSize();

                if (totalSize >= array.Length)
                {
                    throw new Exception("Array is full!");
                }
                //Expand the current stack;
                int extension = Math.Min(s.GetSize(), array.Length - totalSize);

                Shift(s.pointer, totalSize, extension);
                //Shift bound of the arrays following the current one.
                for (int i = stackIndex + 1; i < stacks.Length; i++)
                {
                    stacks[i].start   += extension;
                    stacks[i].end     += extension;
                    stacks[i].pointer += extension;
                }
                s.end += extension;//Expand the current array.
                if (!s.Push(num))
                {
                    throw new Exception("Shift operation failed...check your code.");
                }
            }
            public MultipleStack(int numOfStack, int totalSize)
            {
                this.numOfStack = numOfStack;
                if (totalSize < numOfStack)
                    throw new Exception("Total size is too small.");

                array = new int[totalSize];
                stacks = new SingleStack[numOfStack];

                for (int i = 0; i < numOfStack; i++)
                {
                    //initialize each stack to the size of 1.
                    stacks[i] = new SingleStack(array, i, i + 1);
                }
            }
Пример #4
0
            public MultipleStack(int numOfStack, int totalSize)
            {
                this.numOfStack = numOfStack;
                if (totalSize < numOfStack)
                {
                    throw new Exception("Total size is too small.");
                }

                array  = new int[totalSize];
                stacks = new SingleStack[numOfStack];

                for (int i = 0; i < numOfStack; i++)
                {
                    //initialize each stack to the size of 1.
                    stacks[i] = new SingleStack(array, i, i + 1);
                }
            }