Ejemplo n.º 1
0
        public bool Remove(T item)
        {
            // possible things that this needs to deal with. 1) there is no node to begin with (it is empty) 2) there is only one node in the list
            // 3) the list contains many nodes and we need to remove either the first, or an item from middle or last
            LaurasLinkedListNode <T> previous = null;
            LaurasLinkedListNode <T> current  = Head;

            while (current != null)
            {
                if (current.Value.Equals(item))
                {
                    if (previous != null)
                    {
                        // this means there is more than one item in the list
                        // set the next point of previous to the next pointer of current (as current is going away)
                        previous.Next = current.Next;
                        if (current.Next == null)
                        {
                            // this means that now we've reached the tail
                            Tail = previous;
                        }
                        Count--;
                    }
                    else
                    {
                        RemoveFirst();
                    }
                    return(true);
                }
                previous = current;
                current  = current.Next;
            }

            return(false);
        }
Ejemplo n.º 2
0
        IEnumerator IEnumerable.GetEnumerator()
        {
            LaurasLinkedListNode <T> current = Head;

            while (current != null)
            {
                yield return(current.Value);

                current = current.Next;
            }
        }
Ejemplo n.º 3
0
        public void CopyTo(T[] array, int arrayIndex)
        {
            // copy all elements from the linked list into the array.
            LaurasLinkedListNode <T> current = Head;

            while (current != null)
            {
                array[arrayIndex++] = current.Value;
                current             = current.Next;
            }
        }
Ejemplo n.º 4
0
 public void RemoveFirst()
 {
     if (Count != 0)
     {
         Head = Head.Next;
         Count--;
         if (Count == 0)
         {
             Tail = null;
         }
     }
 }
Ejemplo n.º 5
0
        public void AddFirst(LaurasLinkedListNode <T> node)
        {
            LaurasLinkedListNode <T> temp = Head;

            Head      = node;
            Head.Next = temp;
            Count++;
            if (Count == 1)
            {
                Tail = Head;
            }
        }
Ejemplo n.º 6
0
 public void AddLast(LaurasLinkedListNode <T> node)
 {
     if (Count == 0)
     {
         Head = node;
     }
     else
     {
         Tail.Next = node;
     }
     Tail = node;
     Count++;
 }
Ejemplo n.º 7
0
        public bool Contains(T item)
        {
            LaurasLinkedListNode <T> current = Head;

            while (current != null)
            {
                if (current.Value.Equals(item))
                {
                    return(true);
                }
                current = current.Next;
            }
            return(false);
        }
Ejemplo n.º 8
0
 public void RemoveLast()
 {
     if (Count != 0)
     {
         if (Count == 1)
         {
             Head = null;
             Tail = null;
         }
         else
         {
             // item1, item2, item3
             // want to remove item3. So set Tail = item2
             LaurasLinkedListNode <T> current = Head;
             while (current.Next != Tail)
             {
                 current = current.Next;
             }
         }
         Count--;
     }
 }