コード例 #1
0
        public void SortDesc()
        {
            DoublyNode <T> firstItem;
            DoublyNode <T> secondItem;

            if (_head?.Next == null)
            {
                return;
            }

            var swap = true;

            while (swap)
            {
                swap      = false;
                firstItem = _head;
                while (firstItem.Next != null)
                {
                    secondItem = firstItem.Next;
                    if (firstItem.Data.CompareTo(secondItem.Data) < 0)
                    {
                        firstItem.Next = secondItem.Next;

                        if (secondItem.Next != null)
                        {
                            secondItem.Next.Previous = firstItem;
                        }

                        secondItem.Next     = firstItem;
                        secondItem.Previous = firstItem.Previous;

                        if (secondItem.Previous != null)
                        {
                            secondItem.Previous.Next = secondItem;
                        }
                        else
                        {
                            _head = secondItem;
                        }

                        firstItem.Previous = secondItem;

                        firstItem = secondItem;
                        swap      = true;
                    }

                    firstItem = firstItem.Next;
                }
            }
        }
コード例 #2
0
        public void Add(T data)
        {
            var node = new DoublyNode <T>(data);

            if (_head == null)
            {
                _head = node;
            }
            else
            {
                _tail.Next    = node;
                node.Previous = _tail;
            }

            _tail = node;
            Count++;
        }
コード例 #3
0
        public void AddFirst(T data)
        {
            var node = new DoublyNode <T>(data);
            var temp = _head;

            node.Next = temp;
            _head     = node;
            if (Count == 0)
            {
                _tail = _head;
            }
            else
            {
                temp.Previous = node;
            }
            Count++;
        }
コード例 #4
0
        public void AddRange(params T[] list)
        {
            foreach (var data in list)
            {
                var node = new DoublyNode <T>(data);

                if (_head == null)
                {
                    _head = node;
                }
                else
                {
                    _tail.Next    = node;
                    node.Previous = _tail;
                }

                _tail = node;
                Count++;
            }
        }
コード例 #5
0
        public bool Remove(T data)
        {
            var current = _head;

            while (current != null)
            {
                if (current.Data.Equals(data))
                {
                    break;
                }

                current = current.Next;
            }

            if (current != null)
            {
                if (current.Next != null)
                {
                    current.Next.Previous = current.Previous;
                }
                else
                {
                    _tail = current.Previous;
                }

                if (current.Previous != null)
                {
                    current.Previous.Next = current.Next;
                }
                else
                {
                    _head = current.Next;
                }

                Count--;
                return(true);
            }

            return(false);
        }
コード例 #6
0
 public void Clear()
 {
     _head = null;
     _tail = null;
     Count = 0;
 }