Exemplo n.º 1
0
        public void Push(T value)
        {
            _count++;
            var newNode = new DoublySungNode <T>(value)
            {
                Next = _list.Head
            };

            _list.Head = newNode;
        }
        public DoublySungNode <T> InsertAt(DoublySungNode <T> node, T value)
        {
            var newNode = new DoublySungNode <T>(value)
            {
                Next     = node.Next,
                Previous = node
            };

            if (node.Next != null)
            {
                node.Next.Previous = newNode;
            }
            node.Next = newNode;

            return(newNode);
        }
        public DoublySungNode <T> Append(T value)
        {
            var newNode = new DoublySungNode <T>(value);

            if (Head == null)
            {
                Head = newNode;
            }
            else
            {
                var lastNode = Traverse().LastOrDefault();
                lastNode.Next    = newNode;
                newNode.Previous = lastNode;
            }

            return(newNode);
        }
        public void Remove(DoublySungNode <T> node)
        {
            if (Head.Equals(node))
            {
                Head = Head.Next;
            }
            else
            {
                if (node.Previous != null)
                {
                    node.Previous.Next = node.Next;
                }

                if (node.Next != null)
                {
                    node.Next.Previous = node.Previous;
                }
            }
        }
        public IEnumerable <DoublySungNode <T> > Traverse(Func <DoublySungNode <T>, DoublySungNode <T>, bool> callback = null)
        {
            var current             = Head;
            DoublySungNode <T> last = null;

            while (current != null)
            {
                var callbackResult = callback != null && callback(current, last);
                if (callbackResult)
                {
                    break;
                }

                yield return(current);

                last    = current;
                current = current.Next;
            }
        }
 protected bool Equals(DoublySungNode <T> other)
 {
     return(EqualityComparer <T> .Default.Equals(Value, other.Value) && Equals(Next, other.Next) && Equals(Previous, other.Previous));
 }
 public DoublySungNode(T value)
 {
     Value    = value;
     Next     = null;
     Previous = null;
 }