public void Enqueue(T item)
        {
            if (Count == 0)
            {
                list.AddFirst(item);
                Count++;
                return;
            }
            GenericLinkedNode <T> currentNode = list.First;
            int index = 0;

            while (index <= Count)
            {
                if (item.CompareTo(currentNode.Value) < 0)
                {
                    list.AddBefore(currentNode, item);
                    Count++;
                    return;
                }
                index++;
                currentNode = currentNode.NextNode;
            }
            list.AddLast(item);
            Count++;
        }
Пример #2
0
        void AddTheFirstNode(T value)
        {
            GenericLinkedNode <T> node = new GenericLinkedNode <T>(value);

            First             = node;
            Last              = node;
            node.NextNode     = node;
            node.PreviousNode = node;
            Count++;
        }
Пример #3
0
        public IEnumerator <T> GetEnumerator()
        {
            GenericLinkedNode <T> currentNode = First;

            for (int i = 0; i < Count; i++)
            {
                yield return(currentNode.Value);

                currentNode = currentNode.NextNode;
            }
        }
Пример #4
0
 void PrintList(int i, GenericLinkedNode <T> node)
 {
     if (i == Count - 1)
     {
         return;
     }
     else
     {
         Console.WriteLine(node.Value);
         PrintList(i += 1, node.NextNode);
     }
 }
Пример #5
0
        public void AddBefore(GenericLinkedNode <T> node, T value)
        {
            GenericLinkedNode <T> newNode = new GenericLinkedNode <T>(value);

            newNode.NextNode     = node;
            newNode.PreviousNode = node.PreviousNode;
            node.PreviousNode    = newNode;
            if (node == First)
            {
                First = newNode;
            }
            Count++;
        }
Пример #6
0
 public void AddLast(T value)
 {
     if (First == null)
     {
         AddTheFirstNode(value);
     }
     else
     {
         GenericLinkedNode <T> node = new GenericLinkedNode <T>(value);
         node.PreviousNode = Last;
         node.NextNode     = Last.NextNode;
         Last.NextNode     = node;
         Last = node;
         Count++;
     }
 }
Пример #7
0
        public void AddAfter(GenericLinkedNode <T> node, T value)
        {
            GenericLinkedNode <T> newNode = new GenericLinkedNode <T>(value);

            newNode.PreviousNode = node;
            newNode.NextNode     = node.NextNode;
            node.NextNode        = newNode;
            node.PreviousNode    = newNode;
            if (Count == 1)
            {
                Last = newNode;
            }
            else if (node == Last)
            {
                Last = newNode;
            }
            Count++;
        }
Пример #8
0
        public GenericLinkedNode <T> Find(T value)
        {
            GenericLinkedNode <T> currentNode = First;
            int index = 0;

            while (index < Count)
            {
                if (value.Equals(currentNode.Value))
                {
                    return(currentNode);
                }
                else
                {
                    index++;
                    currentNode = currentNode.NextNode;
                }
            }
            return(null);
        }
Пример #9
0
        public bool Contains(T value)
        {
            GenericLinkedNode <T> currentNode = First;
            int index = 0;

            while (index < Count)
            {
                if (value.Equals(currentNode.Value))
                {
                    return(true);
                }
                else
                {
                    index++;
                    currentNode = currentNode.NextNode;
                }
            }
            return(false);
        }
Пример #10
0
 public void RemoveFirst()
 {
     if (Count == 0)
     {
         return;
     }
     if (Count == 1)
     {
         First = null;
         Last  = null;
         Count--;
     }
     else
     {
         GenericLinkedNode <T> temp = First.NextNode;
         temp.PreviousNode = Last;
         Last.NextNode     = temp;
         First             = temp;
         Count--;
     }
 }
Пример #11
0
 public void Clear()
 {
     Count = 0;
     First = null;
     Last  = null;
 }
Пример #12
0
 public GenericLinkedNode(T value, GenericLinkedNode <T> nextValue, GenericLinkedNode <T> previousValue)
 {
     this.Value        = value;
     this.NextNode     = nextValue;
     this.PreviousNode = previousValue;
 }