Beispiel #1
0
        public System.Collections.Generic.IEnumerable <MyNode> MyEnumerator()
        {
            MyNode currentNode = representant;

            for (int x = 0; x < countElements; x++)
            {
                currentNode = currentNode.GetRightNode();
                yield return(currentNode);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Delete one element in linkedList
        /// </summary>
        /// <param name="node">The node we want to remove</param>
        /// <returns>true if element has been removed, otherwise false</returns>
        public bool DeleteElement(MyNode node)
        {
            // Empty linkedList
            if (representant == null)
            {
                return(false);
            }

            // We have to delete our representant
            if (representant == node)
            {
                // Only one element in linkedList
                if (countElements == 1)
                {
                    representant = null;
                    node.ResetNode();
                    countElements--;
                    return(true);
                }

                // We have to choose new representant
                MyNode tempRepresentant = representant.GetRightNode();
                representant.GetLeftNode().SetRightNode(representant.GetRightNode());
                representant.GetRightNode().SetLeftNode(representant.GetLeftNode());
                representant = tempRepresentant;
                node.ResetNode();

                countElements--;
                return(true);
            }

            // Not representant
            node.GetLeftNode().SetRightNode(node.GetRightNode());
            node.GetRightNode().SetLeftNode(node.GetLeftNode());
            node.ResetNode();

            countElements--;

            return(true);
        }
Beispiel #3
0
        public string ToString()
        {
            string text        = "Linked list - " + countElements + "\n";
            MyNode currentNode = representant;

            if (currentNode == null)
            {
                text += "Empty list";
                return(text += "\n");
            }

            for (int i = 0; i < countElements; i++)
            {
                text       += currentNode + "\n";
                currentNode = currentNode.GetRightNode();
            }

            return(text += "\n");
        }