Beispiel #1
0
        IEnumerator <T> IEnumerable <T> .GetEnumerator()
        {
            DoublyNode <T> current = head;

            while (current != null)
            {
                yield return(current.Data);

                current = current.Next;
            }
        }
Beispiel #2
0
        public IEnumerable <T> BackEnumerator() // перебор элементов списка с конца
        {
            DoublyNode <T> current = tail;

            while (current != null)
            {
                yield return(current.Data);

                current = current.Previous;
            }
        }
Beispiel #3
0
        public bool Contains(T data)
        {
            DoublyNode <T> current = head;

            while (current != null)
            {
                if (current.Data.Equals(data))
                {
                    return(true);
                }
                current = current.Next;
            }
            return(false);
        }
Beispiel #4
0
        public void Add(T data)
        {
            DoublyNode <T> node = new DoublyNode <T>(data);

            if (head == null)
            {
                head = node;
            }
            else
            {
                tail.Next     = node;
                node.Previous = tail;
            }
            tail = node;
            count++;
        }
Beispiel #5
0
        public void AddFirst(T data)
        {
            DoublyNode <T> node = new DoublyNode <T>(data);
            DoublyNode <T> temp = head;

            node.Next = temp;
            head      = node;
            if (count == 0)
            {
                tail = head;
            }
            else
            {
                temp.Previous = node;
            }
            count++;
        }
Beispiel #6
0
        private DoublyNode <T> GetNode(int index)
        {
            if (index < 0 || index >= Count)
            {
                throw new IndexOutOfRangeException($"Index {index} is invalid; Only {Count} elements exist");
            }

            DoublyNode <T> current = head;
            int            t       = 0;

            while (t < index)
            {
                current = current.Next;
                t++;
            }


            return(current);
        }
Beispiel #7
0
        public void Remove(int index)
        {
            var node = GetNode(index);

            if (head == node)
            {
                if (head.Next != null)
                {
                    head.Next.Previous = null;
                    head = head.Next;
                }
                else
                {
                    head = null;
                }
            }
            else if (tail == node)
            {
                if (tail.Previous != null)
                {
                    tail.Previous.Next = null;
                    tail = tail.Previous;
                }
                else
                {
                    tail = null;
                }
            }
            else
            {
                node.Previous.Next = node.Next;
                node.Next.Previous = node.Previous;
            }

            count--;
        }
Beispiel #8
0
 public void Clear()
 {
     head  = null;
     tail  = null;
     count = 0;
 }