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); }
public IEnumerator <T> GetEnumerator() { QueueFromListItem <T> pointer = _head; while (pointer != null) { yield return(pointer.Data); pointer = pointer.NextItem; } }
/// <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); } }
/// <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]); } } }
/// <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); }
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)); }
public QueueFromListItem <T> SetNext(T value) { NextItem = new QueueFromListItem <T>(value); return(NextItem); }
public QueueFromListItem(T value) { Data = value; NextItem = null; }
/// <summary> /// Delete all the items in a queue. /// </summary> public void Clear() { _size = 0; _tail = _head = null; }