Ejemplo n.º 1
0
        public override string ToString()
        {
            StringBuilder output = new StringBuilder();

            if (this.First != null)
            {
                DoublyLinkedListNode <T> currentElement = this.First;

                while (currentElement != null)
                {
                    output.Append(currentElement.Value + " ");

                    currentElement = currentElement.Next;
                }
            }

            return(output.ToString());
        }
Ejemplo n.º 2
0
        public void AddLast(T value)
        {
            DoublyLinkedListNode <T> elementToAdd = new DoublyLinkedListNode <T>(value);

            if (this.Last != null)
            {
                DoublyLinkedListNode <T> previousLastElement = this.Last;
                this.Last.Next = elementToAdd;
                this.Last      = previousLastElement.Next;
                this.Last.Prev = previousLastElement;
            }
            else
            {
                this.Last  = elementToAdd;
                this.First = this.Last;
            }

            this.Count++;
        }
Ejemplo n.º 3
0
        public DoublyLinkedListNode <T> Find(T value)
        {
            if (this.First != null)
            {
                DoublyLinkedListNode <T> currentElement = this.First;

                while (currentElement != null)
                {
                    if (currentElement.Value.CompareTo(value) == 0)
                    {
                        return(currentElement);
                    }

                    currentElement = currentElement.Next;
                }

                return(null);
            }

            throw new InvalidOperationException("Cannot search in empty linked list.");
        }
Ejemplo n.º 4
0
        public void AddAfter(T elementToAddAfter, T value)
        {
            DoublyLinkedListNode <T> elementToAdd = new DoublyLinkedListNode <T>(value);

            if (this.First != null)
            {
                DoublyLinkedListNode <T> currentElement = this.First;

                while (currentElement != null)
                {
                    if (currentElement.Value.CompareTo(elementToAddAfter) == 0)
                    {
                        DoublyLinkedListNode <T> addBefore = currentElement.Next;

                        currentElement.Next = elementToAdd;
                        elementToAdd.Prev   = currentElement;
                        elementToAdd.Next   = addBefore;

                        if (addBefore != null)
                        {
                            addBefore.Prev = elementToAdd;
                        }

                        this.Count++;
                        return;
                    }

                    currentElement = currentElement.Next;
                }

                throw new ArgumentException($"There is no such element as: {elementToAddAfter}");
            }
            else
            {
                throw new ArgumentException("Cannot add after in empty linked list.");
            }
        }
Ejemplo n.º 5
0
 public DoublyLinkedListNode(T value)
 {
     this.Value = value;
     this.Next  = null;
     this.Prev  = null;
 }
Ejemplo n.º 6
0
 public DoublyLinkedList(T value)
 {
     this.First = new DoublyLinkedListNode <T>(value);
     this.Last  = this.First;
     this.Count = 1;
 }
Ejemplo n.º 7
0
 public DoublyLinkedList()
 {
     this.First = null;
     this.Last  = null;
     this.Count = 0;
 }
Ejemplo n.º 8
0
 public void Clear()
 {
     this.First = null;
     this.Last  = null;
     this.Count = 0;
 }