Beispiel #1
0
        public void AppendTo(Stack <AnyType> Destination)
        {
            if (Destination == null)
            {
                throw new CollectionsDataNullException("Stack.cs", "Stack<AnyType>", "AppendTo", "Destination", "Destination cannot be NULL!");
            }

            StackNode <AnyType> NodePointer = FirstNode;

            while (NodePointer != null)
            {
                Destination.Push(NodePointer.Data);

                NodePointer = NodePointer.Next;
            }
        }
Beispiel #2
0
        public void AppendFrom(Stack <AnyType> Source)
        {
            if (Source == null)
            {
                throw new CollectionsDataNullException("Stack.cs", "Stack<AnyType>", "AppendFrom", "Source", "Source cannot be NULL!");
            }

            StackNode <AnyType> NodePointer = Source.FirstNode;

            while (NodePointer != null)
            {
                Push(NodePointer.Data);

                NodePointer = NodePointer.Next;
            }
        }
Beispiel #3
0
        public AnyType[] ToArray()
        {
            AnyType[]           ConvertedArray = new AnyType[Count];
            StackNode <AnyType> NodePointer    = FirstNode;
            int Counter = -1;

            while (NodePointer != null)
            {
                Counter++;

                ConvertedArray[Counter] = NodePointer.Data;

                NodePointer = NodePointer.Next;
            }

            return(ConvertedArray);
        }
Beispiel #4
0
        public StackNode <AnyType> Push(AnyType Data)
        {
            StackNode <AnyType> NewNode = new StackNode <AnyType>(Data);

            if (FirstNode == null && LastNode == null && Count == 0)
            {
                NewNode.Prev = null;
                NewNode.Next = null;

                FirstNode = NewNode;
                LastNode  = NewNode;

                Count++;

                return(NewNode);
            }

            if (FirstNode != null && LastNode != null && FirstNode == LastNode && Count == 1)
            {
                NewNode.Prev = LastNode;
                NewNode.Next = null;

                LastNode.Next = NewNode;
                LastNode      = NewNode;

                Count++;

                return(NewNode);
            }

            if (FirstNode != null && LastNode != null && FirstNode != LastNode && Count > 1)
            {
                NewNode.Prev = LastNode;
                NewNode.Next = null;

                LastNode.Next = NewNode;
                LastNode      = NewNode;

                Count++;

                return(NewNode);
            }

            return(null);
        }
Beispiel #5
0
        public StackNode <AnyType> FindLast(StackNode <AnyType> Node)
        {
            StackNode <AnyType> NodePointer = LastNode;

            while (NodePointer != null)
            {
                if (NodePointer == Node)
                {
                    return(NodePointer);
                }
                else
                {
                    NodePointer = NodePointer.Prev;
                }
            }

            return(NodePointer);
        }
Beispiel #6
0
        public StackNode <AnyType> FindFirst(StackNode <AnyType> Node)
        {
            StackNode <AnyType> NodePointer = FirstNode;

            while (NodePointer != null)
            {
                if (NodePointer == Node)
                {
                    return(NodePointer);
                }
                else
                {
                    NodePointer = NodePointer.Next;
                }
            }

            return(NodePointer);
        }
Beispiel #7
0
        public StackNode <AnyType> FindLast(AnyType Data)
        {
            StackNode <AnyType> NodePointer = LastNode;

            while (NodePointer != null)
            {
                if (NodePointer.Data.CompareTo(Data) == 0)
                {
                    return(NodePointer);
                }
                else
                {
                    NodePointer = NodePointer.Prev;
                }
            }

            return(NodePointer);
        }
Beispiel #8
0
        public void RemoveAll(AnyType Data)
        {
            StackNode <AnyType> NodePointer = FirstNode;
            StackNode <AnyType> BackupNode  = null;

            while (NodePointer != null)
            {
                BackupNode = NodePointer.Next;

                if (NodePointer.Data.CompareTo(Data) == 0)
                {
                    RemoveNode(NodePointer);

                    Count--;
                }

                NodePointer = BackupNode;
            }
        }
Beispiel #9
0
        public bool RemoveLast(AnyType Data)
        {
            StackNode <AnyType> NodePointer = LastNode;

            while (NodePointer != null)
            {
                if (NodePointer.Data.CompareTo(Data) == 0)
                {
                    RemoveNode(NodePointer);

                    Count--;

                    return(true);
                }

                NodePointer = NodePointer.Prev;
            }

            return(false);
        }
Beispiel #10
0
        public void Clear()
        {
            StackNode <AnyType> NodePointer = FirstNode;
            StackNode <AnyType> BackupNode  = null;

            while (NodePointer != null && Count > 0)
            {
                BackupNode = NodePointer.Next;

                NodePointer = null;

                Count--;

                NodePointer = BackupNode;
            }

            FirstNode   = null;
            LastNode    = null;
            CurrentNode = null;
            Count       = 0;
        }
Beispiel #11
0
        public void SortWithBinarySearchTree()
        {
            BinarySearchTree <AnyType> BSTree      = new BinarySearchTree <AnyType>();
            StackNode <AnyType>        NodePointer = FirstNode;

            while (NodePointer != null)
            {
                BSTree.Add(NodePointer.Data);

                NodePointer = NodePointer.Next;
            }

            Clear();

            foreach (AnyType ListItem in BSTree.TraverseMinToMax())
            {
                Push(ListItem);
            }

            BSTree.Clear();
            BSTree = null;
        }
Beispiel #12
0
        public bool TryPop(out AnyType Item)
        {
            Item = default(AnyType);

            if (FirstNode == null && LastNode == null && Count == 0)
            {
                return(false);
            }

            if (FirstNode != null && LastNode != null && FirstNode == LastNode && Count == 1)
            {
                Item = FirstNode.Data;

                FirstNode = null;
                LastNode  = null;

                Count--;

                return(true);
            }

            if (FirstNode != null && LastNode != null && FirstNode != LastNode && Count > 1)
            {
                Item = LastNode.Data;

                StackNode <AnyType> BackupNode = LastNode;

                LastNode      = BackupNode.Prev;
                LastNode.Next = null;

                BackupNode = null;

                Count--;

                return(true);
            }

            return(false);
        }
Beispiel #13
0
        public AnyType Pop()
        {
            if (FirstNode == null && LastNode == null && Count == 0)
            {
                throw new CollectionsDataNotFoundException("Stack.cs", "Stack<AnyType>", "Pop", "", "Stack is empty! No data exists in Stack!");
            }

            if (FirstNode != null && LastNode != null && FirstNode == LastNode && Count == 1)
            {
                AnyType Result = FirstNode.Data;

                FirstNode = null;
                LastNode  = null;

                Count--;

                return(Result);
            }

            if (FirstNode != null && LastNode != null && FirstNode != LastNode && Count > 1)
            {
                AnyType Result = LastNode.Data;

                StackNode <AnyType> BackupNode = LastNode;

                LastNode      = BackupNode.Prev;
                LastNode.Next = null;

                BackupNode = null;

                Count--;

                return(Result);
            }

            throw new CollectionsUnknownErrorException("Stack.cs", "Stack<AnyType>", "Pop", "", "A Stack has either the size of 0, 1 or more than 1. This unknown exception should not execute!");
        }
Beispiel #14
0
 void IEnumerator.Reset()
 {
     EnumNode = null;
 }
Beispiel #15
0
 public bool NotContains(StackNode <AnyType> Node)
 {
     return(FindFirst(Node) == null);
 }
Beispiel #16
0
 public StackNode <AnyType> Find(StackNode <AnyType> Node)
 {
     return(FindLast(Node));
 }
Beispiel #17
0
        public bool RemoveNode(StackNode <AnyType> Node)
        {
            if (Node == null)
            {
                throw new CollectionsDataNullException("Stack.cs", "Stack<AnyType>", "Delete", "Node", "Node cannot be NULL!");
            }

            if (NotContains(Node))
            {
                throw new CollectionsDataNotFoundException("Stack.cs", "Stack<AnyType>", "Delete", "Node", "Node cannot be found in this Stack!");
            }

            if (Node.Prev == null && Node.Next == null && Count == 1)
            {
                FirstNode = null;
                LastNode  = null;

                Node = null;

                Count--;

                return(true);
            }

            if (Node.Prev == null && Node.Next != null && Count > 1)
            {
                FirstNode      = Node.Next;
                FirstNode.Prev = null;

                Node = null;

                Count--;

                return(true);
            }

            if (Node.Prev != null && Node.Next == null && Count > 1)
            {
                LastNode      = Node.Prev;
                LastNode.Next = null;

                Node = null;

                Count--;

                return(true);
            }

            if (Node.Prev != null && Node.Next != null && Count > 2)
            {
                Node.Prev.Next = Node.Next;
                Node.Next.Prev = Node.Prev;

                Node = null;

                Count--;

                return(true);
            }

            return(false);
        }