示例#1
0
        private TwoWayNode <T> GetNodeFromIndex(int index)
        {
            TwoWayNode <T> storageNode = firstNode;

            for (int i = 0; i < index; i++)
            {
                storageNode = storageNode.NextNode;
            }

            return(storageNode);
        }
示例#2
0
        public bool Remove(T value)
        {
            TwoWayNode <T> storageNode = firstNode;

            for (int i = 0; i < Count; i++)
            {
                if (storageNode.Value.Equals(value))
                {
                    RemoveAt(i);
                    return(true);
                }

                storageNode = storageNode.NextNode;
            }
            return(false);
        }
示例#3
0
        public void Insert(T value, int index)
        {
            if (index < 0 || index > Count)
            {
                throw new Exception("Index of the List not valid!");
            }
            TwoWayNode <T> nodeToAdd = new TwoWayNode <T>(value, null, null);

            if (firstNode == null)
            {
                firstNode = nodeToAdd;
                lastNode  = nodeToAdd;
            }
            else if (firstNode == lastNode)
            {
                lastNode = nodeToAdd;
                lastNode.PreviousNode = firstNode;
                firstNode.NextNode    = lastNode;
            }
            else
            {
                if (index == 0)
                {
                    firstNode.PreviousNode          = nodeToAdd;
                    firstNode.PreviousNode.NextNode = firstNode;
                    firstNode = firstNode.PreviousNode;
                }
                else if (index == Count)
                {
                    lastNode.NextNode = nodeToAdd;
                    lastNode.NextNode.PreviousNode = lastNode;
                    lastNode = lastNode.NextNode;
                }
                else
                {
                    TwoWayNode <T> previousNode = GetNodeFromIndex(index - 1);
                    TwoWayNode <T> nextNode     = previousNode.NextNode;

                    nextNode.PreviousNode  = nodeToAdd;
                    nodeToAdd.NextNode     = nextNode;
                    nodeToAdd.PreviousNode = previousNode;
                    previousNode.NextNode  = nodeToAdd;
                }
            }
            Count++;
        }
示例#4
0
        public string DisplayBackward()
        {
            if (Count == 0)
            {
                return("Null list");
            }

            string         storageString = "";
            TwoWayNode <T> node          = lastNode;

            for (int i = Count - 1; i >= 0; i--)
            {
                storageString += node.Value.ToString();
                node           = node.PreviousNode;
            }

            return(storageString);
        }
示例#5
0
        // THOSE METHODS ARE ONLY TO SHOW THAT ARE DIFFERENT MANNERS OF SHOWING DATA IN DOUBLY LINKED LISTS.
        // CON -> REQUIRE MORE MEMORY THAN THE SINGLY LINKED LIST
        // It could also be written as a CircularDoublyLinkedList, that, first node previous
        //      ref points to the last one, and it's next points to the first one.

        public string DisplayForward()
        {
            if (Count == 0)
            {
                return("Null list");
            }

            string         storageString = "";
            TwoWayNode <T> node          = firstNode;

            for (int i = 0; i < Count; i++)
            {
                storageString += node.Value.ToString();
                node           = node.NextNode;
            }

            return(storageString);
        }
示例#6
0
        /// <summary>
        /// Clear all list data. O(N)
        /// Here I am not making any data Dispose, though it should be done. I'm just deleting all refs,
        ///     like disposing, but making them null.
        /// </summary>
        public void Clear()
        {
            /*
             * TwoWayNode<T> previousNode = firstNode;
             * TwoWayNode<T> nextNode = firstNode.NextNode;
             *
             * // It should be disposed as this example, although it is not doing anything:
             * for (int i = 0; i < Count - 1; i++)
             * {
             *  previousNode = null;
             *  previousNode = nextNode;
             *  nextNode = nextNode.NextNode;
             * }
             */

            // Only to simplify, I'll clear both first and top nodes, and make the Count 0. Garbage Collector will do the rest
            firstNode = null;
            lastNode  = null;
            Count     = 0;
        }
示例#7
0
        /// <summary>
        /// Remove a data from the List at a specified index.
        /// </summary>
        /// <param name="index">Index delete the data.</param>
        public void RemoveAt(int index)
        {
            if (index < 0 || index >= Count)
            {
                throw new Exception("Invalid index!");
            }

            if (Count == 1)
            {
                firstNode = null;
                lastNode  = null;
            }
            else if (index == 0)
            {
                TwoWayNode <T> nodeToRemove = firstNode;
                firstNode    = nodeToRemove.NextNode;
                nodeToRemove = null;
            }
            else if (index == Count - 1)
            {
                TwoWayNode <T> nodeToRemove = lastNode;
                lastNode     = nodeToRemove.PreviousNode;
                nodeToRemove = null;
            }
            else if (index != 0)
            {
                TwoWayNode <T> nodeToRemove = GetNodeFromIndex(index);
                TwoWayNode <T> previousNode = nodeToRemove.PreviousNode;
                TwoWayNode <T> nextNode     = nodeToRemove.NextNode;

                previousNode.NextNode = nextNode;
                nextNode.PreviousNode = previousNode;
                nodeToRemove          = null;
            }
            Count--;
        }
示例#8
0
 public TwoWayNode(T value, TwoWayNode <T> previousNode, TwoWayNode <T> nextNode)
 {
     this.NextNode     = nextNode;
     this.PreviousNode = previousNode;
     this.Value        = value;
 }
示例#9
0
 public DoublyLinkedList()
 {
     Count     = 0;
     firstNode = null;
     lastNode  = null;
 }