示例#1
0
        public void InsertAt(int position, T value)
        {
            this.ThrowIfInvalidPosition(position);

            if (position == 0)
            {
                if (this.Head == null)
                {
                    DoubleNode <T> node = new DoubleNode <T>(value, null, null);
                    node.Next     = node;
                    node.Previous = node;

                    this.Head = node;
                    this.Tail = node;
                }
                else
                {
                    DoubleNode <T> node = new DoubleNode <T>(value, this.Tail, this.Head);

                    this.Head.Previous = node;
                    this.Head          = node;
                }

                this.Tail !.Next = this.Head;
            }
            else if (position >= this.Count - 1)
            {
                DoubleNode <T> node = new DoubleNode <T>(value, this.Tail, this.Head);

                this.Head !.Previous = node;
                this.Tail !.Next     = node;
                this.Tail            = node;
            }
            else
            {
                DoubleNode <T> parent = this.Head !;

                for (int i = 1; i < position; i++)
                {
                    parent = parent.Next !;
                }

                DoubleNode <T> node = new DoubleNode <T>(value, parent, parent.Next);
                parent.Next !.Previous = node;
                parent.Next            = node;
            }

            this.Count++;
        }
        public void DeleteAt(int position)
        {
            this.ThrowIfEmpty();
            this.ThrowIfInvalidPosition(position);

            if (position == 0)
            {
                DoubleNode <T> head = this.Head !;

                if (head !.Next == null)
                {
                    this.Head !.Next     = null;
                    this.Head            = null;
                    this.Tail !.Previous = null;
                    this.Tail            = null;
                }
                else
                {
                    this.Head          = head.Next;
                    this.Head.Previous = null;

                    head.Next = null;
                }
            }