public T Pop() { if (this.currentNode == null) { throw new Exception("Stack is empty!"); } T result = this.currentNode.Value; DynamicStackNode <T> prev = currentNode.Prev; prev.Next = null; currentNode.Prev = null; currentNode = prev; this.Count--; return(result); }
public void Push(T value) { if (this.currentNode == null) { this.currentNode = new DynamicStackNode <T>(value); this.Count++; return; } DynamicStackNode <T> node = new DynamicStackNode <T>(value); node.Prev = this.currentNode; this.currentNode.Next = node; this.currentNode = node; this.Count++; }
public DynamicStack(IEnumerable <T> collection) { if (collection == null || collection.Count() == 0) { throw new Exception("The provided collection is null or emtpy."); } this.currentNode = new DynamicStackNode <T>(collection.First()); this.Count++; foreach (var value in collection.Skip(1)) { DynamicStackNode <T> node = new DynamicStackNode <T>(value); node.Prev = currentNode; this.currentNode.Next = node; this.currentNode = node; this.Count++; } }
public T[] ToArray() { if (this.currentNode == null) { throw new Exception("Stack is empty!"); } T[] arr = new T[this.Count]; DynamicStackNode <T> node = this.currentNode; int index = this.Count - 1; while (node != null) { arr[index] = node.Value; node = node.Prev; index--; } return(arr); }
public DynamicStack(T value) { this.currentNode = new DynamicStackNode <T>(value); this.Count++; }