Exemplo n.º 1
0
        public void AddFirst(object head)
        {
            var node = new CoolNode(head);

            if (this.Count == 0)
            {
                this.head = this.tail = node;
            }
            else
            {
                var currentHead = this.head;
                currentHead.Prev      = node;
                currentHead.Prev.Next = this.head;
                this.head             = node;
            }
            this.Count++;
        }
Exemplo n.º 2
0
        public void AddLast(object value)
        {
            var node = new CoolNode(value);

            if (this.Count == 0)
            {
                this.tail = this.head = node;
            }
            else
            {
                var currentTail = this.tail;
                currentTail.Next      = node;
                currentTail.Next.Prev = this.tail;
                this.tail             = node;
            }
            this.Count++;
        }
        public void AddTail(object value)
        {
            var newNode = new CoolNode(value);

            if (Count == 0)
            {
                this.Head = this.Tail = new CoolNode(value);
            }
            else
            {
                var oldTail = this.Tail;
                oldTail.Next     = newNode;
                newNode.Previous = oldTail;
                this.Tail        = newNode;
            }

            Count++;
        }
        public void AddHead(object value)
        {
            var newNode = new CoolNode(value);

            if (this.Count == 0)
            {
                this.Head = this.Tail = new CoolNode(value);
            }
            else
            {
                var oldNode = this.Head;
                oldNode.Previous = newNode;
                newNode.Next     = oldNode;
                this.Head        = newNode;
            }

            this.Count++;
        }
Exemplo n.º 5
0
        public object RemoveLast()
        {
            ValidateIfListIsEmpty();
            var result = this.tail.Value;

            if (this.head == this.tail)
            {
                this.head = null;
                this.tail = null;
            }
            else
            {
                var newTail = new CoolNode(this.tail.Prev.Value);
                newTail.Prev = this.tail.Prev.Prev;
                this.tail    = newTail;
                this.Count--;
            }

            return(result);
        }
Exemplo n.º 6
0
        public object RemoveFirst()
        {
            ValidateIfListIsEmpty();
            var result = this.head.Value;

            if (this.head == this.tail)
            {
                this.head = null;
                this.tail = null;
            }
            else
            {
                var newHead = new CoolNode(this.head.Next.Value);
                newHead.Next = this.head.Next.Next;
                this.head    = newHead;
                this.Count--;
            }

            return(result);
        }
Exemplo n.º 7
0
        public object RemoveHead()
        {
            this.ValidateIfListIsEmpty();

            var value = this.head.Value;

            if (this.head == this.tail)
            {
                this.head = null;
                this.tail = null;
            }
            else
            {
                var newHead = this.head.Next;
                newHead.Previous = null;
                this.head.Next   = null;
                this.head        = newHead;
            }

            this.Count--;

            return(value);
        }
Exemplo n.º 8
0
        public void Remove(object value)
        {
            var currentNode = this.head;

            while (currentNode != null)
            {
                var nodeValue = currentNode.Value;

                if (nodeValue.Equals(value))
                {
                    this.Count--;

                    var prevNode = currentNode.Previous;
                    var nextNode = currentNode.Next;

                    if (prevNode != null)
                    {
                        prevNode.Next = nextNode;
                    }

                    if (nextNode != null)
                    {
                        nextNode.Previous = prevNode;
                    }

                    if (this.head == currentNode)
                    {
                        this.head = nextNode;
                    }

                    if (this.tail == currentNode)
                    {
                        this.tail = prevNode;
                    }
                }
            }
        }
Exemplo n.º 9
0
 public void Clear()
 {
     this.head  = null;
     this.tail  = null;
     this.Count = 0;
 }