Esempio n. 1
0
        /// <summary>
        /// remove the tail of the list
        /// </summary>
        /// <returns></returns>
        public bool RemoveLast()
        {
            if (_head == null)
            {
                return(false);
            }

            if (_head == _tail)
            {
                _head = null;
                _tail = null;
                return(true);
            }

            var prev = _head;

            while (prev.Next != _tail)
            {
                prev = prev.Next;
            }

            prev.Next = null;
            _tail     = prev;
            return(true);
        }
Esempio n. 2
0
 /// <summary>
 /// Add a node after a specific node
 /// both cannot be null
 /// </summary>
 /// <param name="node"></param>
 /// <param name="newNode"></param>
 public void AddAfter(LListNode <T> node, LListNode <T> newNode)
 {
     if (node == _tail)
     {
         AddLast(newNode);
     }
     else
     {
         newNode.Next = node.Next;
         node.Next    = newNode;
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Return the number of items in the list
        /// </summary>
        /// <returns></returns>
        public int Count()
        {
            LListNode <T> current = _head;
            int           count   = 0;

            while (current != null)
            {
                count++;
                current = current.Next;
            }

            return(count);
        }
Esempio n. 4
0
 /// <summary>
 /// Add a node to the rear
 /// </summary>
 /// <param name="node"></param>
 public void AddLast(LListNode <T> node)
 {
     if (_head == null)
     {
         _head = node;
         _tail = node;
     }
     else
     {
         _tail.Next = node;
         _tail      = node;
     }
 }
Esempio n. 5
0
 /// <summary>
 /// Add a LList Node to the front
 /// </summary>
 /// <param name="node"></param>
 public void AddFirst(LListNode <T> node)
 {
     if (_head == null)
     {
         _head = node;
         _tail = node;
     }
     else
     {
         node.Next = _head;
         _head     = node;
     }
 }
Esempio n. 6
0
        /// <summary>
        /// Add a node before specific node
        /// </summary>
        /// <param name="node"></param>
        /// <param name="newNode"></param>
        public void AddBefore(LListNode <T> node, LListNode <T> newNode)
        {
            if (node == _head)
            {
                AddFirst(newNode);
                return;
            }
            var current = _head;

            while (current.Next != node)
            {
                current = current.Next;
            }
            newNode.Next = node;
            current.Next = newNode;
        }
Esempio n. 7
0
        /// <summary>
        /// Override from System.Object, convert the list to a string
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            LListNode <T> current = _head;
            string        result  = "";

            while (current != null)
            {
                result += current.Value.ToString();
                if (current.Next != null)
                {
                    result += ",";
                }
                current = current.Next;
            }

            return(result);
        }
Esempio n. 8
0
        /// <summary>
        /// remove the head of list
        /// </summary>
        /// <returns></returns>
        public bool RemoveFirst()
        {
            if (_head == null)
            {
                return(false);
            }

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

            _head = _head.Next;
            return(true);
        }
Esempio n. 9
0
        /// <summary>
        /// Find the last appearance of a value and return the node
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public LListNode <T> FindLast(T value)
        {
            if (_head == null)
            {
                return(null);
            }

            var           current = _head;
            LListNode <T> buffer  = null;

            while (current != null)
            {
                if (EqualityComparer <T> .Default.Equals(current.Value, value))
                {
                    buffer = current;
                }
                current = current.Next;
            }

            return(buffer);
        }
Esempio n. 10
0
        /// <summary>
        /// Remove specific node
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool Remove(LListNode <T> node)
        {
            if (_head == null)
            {
                return(false);
            }

            if (_head == _tail && ReferenceEquals(_head, node))
            {
                this.Clear();
                return(true);
            }
            else if (ReferenceEquals(_head, node))
            {
                this.RemoveFirst();
                return(true);
            }
            else if (ReferenceEquals(_tail, node))
            {
                this.RemoveLast();
                return(true);
            }
            else
            {
                var prev = _head;
                while (prev.Next != null)
                {
                    if (ReferenceEquals(prev, node))
                    {
                        prev.Next = prev.Next.Next;
                        return(true);
                    }
                    prev = prev.Next;
                }
                return(false);
            }
        }
Esempio n. 11
0
 /// <summary>
 /// Clear this LList
 /// </summary>
 public void Clear()
 {
     _head = null;
     _tail = null;
 }
Esempio n. 12
0
        /// <summary>
        /// Add a value as a node before specific node
        /// </summary>
        /// <param name="node"></param>
        /// <param name="value"></param>
        public void AddBefore(LListNode <T> node, T value)
        {
            var newNode = new LListNode <T>(value);

            AddBefore(node, newNode);
        }
Esempio n. 13
0
        /// <summary>
        /// Add a value as a node after a specific node
        /// </summary>
        /// <param name="node"></param>
        /// <param name="value"></param>
        public void AddAfter(LListNode <T> node, T value)
        {
            var newNode = new LListNode <T>(value);

            AddAfter(node, newNode);
        }
Esempio n. 14
0
        /// <summary>
        /// Add a value as a node to the rear
        /// </summary>
        /// <param name="value"></param>
        public void AddLast(T value)
        {
            var node = new LListNode <T>(value);

            AddLast(node);
        }