Пример #1
0
        /// <summary>
        /// Helper method for pushing to stack. Gets the previous size of the stack and increments by one, assigning size to pushed node.
        /// </summary>
        /// <param name="pushedNode">StackOfPlatesNode</param>
        /// <param name="stack">StackOfPlatesNode</param>
        /// <returns>StackOfPlatesNode</returns>
        private StackOfPlatesNode PushToIndividualStack(StackOfPlatesNode pushedNode, StackOfPlatesNode stack)
        {
            int stackSize = stack.Size;

            pushedNode.Size = stackSize + 1;
            pushedNode.Next = stack;

            return(pushedNode);
        }
Пример #2
0
        /// <summary>
        /// Helper method that takes an individual stack and tries to get the popped node, returns true upon success and assigns poppedNode, false on failure
        /// </summary>
        /// <param name="stack">StackOfPlatesNode</param>
        /// <param name="poppedNode">StackOfPlatesNode</param>
        /// <returns>bool</returns>
        public bool TryPopFromStack(ref StackOfPlatesNode stack, out StackOfPlatesNode poppedNode)
        {
            if (stack == null)
            {
                poppedNode = null;
                return(false);
            }

            poppedNode = stack;
            stack      = stack.Next;
            return(true);
        }
Пример #3
0
        /// <summary>
        /// Adds new value to top of stack
        /// </summary>
        /// <param name="data">int</param>
        public void Push(int data)
        {
            StackOfPlatesNode pushedNode = new StackOfPlatesNode(data);

            for (int i = Stacks.Length - 1; i >= 0; i--)
            {
                if (TryPushToStack(pushedNode, ref Stacks[i]))
                {
                    return;
                }
            }
            throw new Exception("Stack has exceeded size capacity");
        }
Пример #4
0
 /// <summary>
 /// Helper method for adding value to top of stack. If individual stack is full, returns false. Returns true on success
 /// </summary>
 /// <param name="pushedNode">StackOfPlatesNode</param>
 /// <param name="stack">StackOfPlatesNode</param>
 /// <returns>bool</returns>
 private bool TryPushToStack(StackOfPlatesNode pushedNode, ref StackOfPlatesNode stack)
 {
     if (stack == null)
     {
         pushedNode.Size = 1;
         stack           = pushedNode;
         return(true);
     }
     else if (stack.Size < SizeThreshold)
     {
         stack = PushToIndividualStack(pushedNode, stack);
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #5
0
 public StackOfPlatesNode(int data)
 {
     Data = data;
     Next = null;
 }