/// <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); }
/// <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); }
/// <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"); }
/// <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); } }
public StackOfPlatesNode(int data) { Data = data; Next = null; }