예제 #1
0
        public static bool operator ==(QueueFromList <T> a, QueueFromList <T> b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return(true);
            }

            if (((object)a == null) || ((object)b == null))
            {
                return(false);
            }

            if (a.Count != b.Count)
            {
                return(false);
            }
            QueueFromListItem <T> pointerA = a._head, pointerB = b._head;

            while (pointerA != null)
            {
                if (pointerA != pointerB)
                {
                    return(false);
                }
                pointerA = pointerA.NextItem;
                pointerB = pointerB.NextItem;
            }
            return(true);
        }
예제 #2
0
        public IEnumerator <T> GetEnumerator()
        {
            QueueFromListItem <T> pointer = _head;

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

                pointer = pointer.NextItem;
            }
        }
예제 #3
0
 /// <summary>
 /// Adds item into a queue
 /// </summary>
 /// <param name="item">Item added to queue.</param>
 public void Enqueue(T item)
 {
     _size++;
     if (_head == null)
     {
         _head = new QueueFromListItem <T>(item);
         _tail = _head;
     }
     else
     {
         _tail = _tail.SetNext(item);
     }
 }
예제 #4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="values">Items to be enqueued.</param>
 public QueueFromList(params T[] values)
 {
     _size = 0;
     _tail = _head = null;
     if (values.Length > 0)
     {
         _head = new QueueFromListItem <T>(values[0]);
         _tail = _head;
         _size++;
         for (int i = 1; i < values.Length; i++)
         {
             Enqueue(values[i]);
         }
     }
 }
예제 #5
0
        /// <summary>
        /// Returns an item with removing it.
        /// </summary>
        /// <returns>Item from the head of a queue</returns>
        public T Dequeue()
        {
            if (_size == 0)
            {
                throw new Exception("Queue is empty!");
            }
            T temp = _head.Data;

            if (_size == 1)
            {
                _tail = _head = null;
            }
            else
            {
                _head = _head.NextItem;
            }
            _size--;
            return(temp);
        }
예제 #6
0
        public override bool Equals(object obj)
        {
            if (this == null)
            {
                throw new NullReferenceException();
            }
            if (obj == null)
            {
                return(false);
            }

            QueueFromListItem <T> p = obj as QueueFromListItem <T>;

            if ((object)p == null)
            {
                return(false);
            }

            return(this.Data.Equals(p.Data));
        }
예제 #7
0
 public QueueFromListItem <T> SetNext(T value)
 {
     NextItem = new QueueFromListItem <T>(value);
     return(NextItem);
 }
예제 #8
0
 public QueueFromListItem(T value)
 {
     Data     = value;
     NextItem = null;
 }
예제 #9
0
 /// <summary>
 /// Delete all the items in a queue.
 /// </summary>
 public void Clear()
 {
     _size = 0;
     _tail = _head = null;
 }