Example #1
0
        /// <summary>
        /// This is the C# way to iterate (enumerate) elements
        /// </summary>
        /// <returns></returns>
        IEnumerator <T> IEnumerable <T> .GetEnumerator()
        {
            SNode <T> current = Head;

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

                current = current.Next;
            }
        }
Example #2
0
        public SNode <T> Remove(T item)
        {
            SNode <T> found = FindPrevious(item);

            if (found != null)
            {
                found.Next = found.Next.Next;
                Count--;
            }

            return(found);
        }
Example #3
0
        private void Reverse(SNode <T> node)
        {
            if (node == null)
            {
                return;
            }

            Reverse(node.Next);

            RemoveHead();
            Append(node.Data);
        }
Example #4
0
        public void Prepend(T item)
        {
            SNode <T> newNode = new SNode <T>(item);

            newNode.Next = Head;
            Head         = newNode;

            if (Count == 0)
            {
                Tail = Head;
            }

            Count++;
        }
Example #5
0
        public void Append(T item)
        {
            if (Count == 0)
            {
                Head = new SNode <T>(item);
                Tail = Head;
            }
            else
            {
                Tail = Tail.InsertAfter(item);
            }

            Count++;
        }
Example #6
0
        public SNode <T> Find(T item)
        {
            var iter = GetIterator();

            while (iter.IsValid())
            {
                SNode <T> curr = iter.GetNode();

                if (EqualityComparer <T> .Default.Equals(curr.Data, item))
                {
                    return(curr);
                }

                iter.MoveNext();
            }
            return(null);
        }
Example #7
0
        private SNode <T> FindPrevious(T item)
        {
            var iter = GetIterator();

            while (iter.IsValid())
            {
                SNode <T> curr = iter.GetNode();

                if (curr.Next != null && EqualityComparer <T> .Default.Equals(curr.Next.Data, item))
                {
                    return(curr);
                }

                iter.MoveNext();
            }

            return(null);
        }
Example #8
0
 public SListIterator(SLinkedList <T> list)
 {
     Current = list.Head;
     Head    = list.Head;
 }