Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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++;
        }
Ejemplo n.º 3
0
        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++;
            }
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
 public DynamicStack(T value)
 {
     this.currentNode = new DynamicStackNode <T>(value);
     this.Count++;
 }