/// <summary> /// Traverse the whole stack /// </summary> public void Traverse() { LinkedNode <T> cursor = head; while (cursor != null) { Screen.WriteLine(cursor.Data.ToString()); cursor = cursor.next; } }
/// <summary> /// returns the head and removes it /// </summary> /// <returns></returns> public T Pop() { if (head == null) { throw new Exception("the stack is empty"); } LinkedNode <T> node = new LinkedNode <T>(head.Data); head = head.next; return(node.Data); }
/// <summary> /// Add a new Item to the Stack /// </summary> /// <param name="value"></param> public void Push(T value) { var node = new LinkedNode <T>(value); if (head != null) { node.next = head; } else { node.next = null; } head = node; }
/// <summary> /// Helper Method to get a Node from Index /// </summary> /// <param name="index"></param> /// <returns></returns> private LinkedNode <T> GetNodeFromIndex(int index) { int currIndex = -1; LinkedNode <T> node = head; while (currIndex <= index) { currIndex = currIndex + 1; node = node.next; if (currIndex == index) { return(node); } } return(null); }