Question: In main method, create a queue that has it's data crossing the gap of death. In other words, the data in the queue wraps around
In main method, create a queue that has it's data crossing the "gap of death". In other words, the data in the queue wraps around the end of the array. In other words, part of the data is stored in the back of the array and part in the front of the array.
Create an Contains method to add to the OurQueue class. It must return true if the value passed to the method is in the Queue, otherwise it must return false.
using System; using System.Collections.Generic;
// Student Version namespace ConsoleApplication1 { ///
/// A Queue class using a "circular" array ///
/// class OurQueue : IEnumerable { /// /// The front points to the first empty cell /// The end points to the last cell with data /// The size is the number of items in the queue /// private int front, end, size; /// /// The array holds the data but the data is from front to end and NOT always starting at array position 0 /// private T[] myArray;
///
/// Construct an emtpy queue with a specified size /// /// public OurQueue(int capacity = 10) { myArray = new T[capacity]; }
///
/// Clears or "removes" the contents of the queue /// /// Doesn't actually erase anything just makes it inaccessible and write over it later public void Clear() { front = 0; end = 0; size = 0; }
///
/// Since the user can't look at the private variable, we need to provide a way to tell if the queue is empty /// /// Returns true if empty; otherwise returns false public bool IsEmpty() { return size == 0; }
///
/// Removes and returns the item in the front of the queue /// /// public T Dequeue() { size--; T data = myArray[front]; Increment(ref front); return data; } /// /// Inserts the new item at the end of the queue /// /// public void Enqueue(T newItem) { if (this.size < myArray.Length) { myArray[end] = newItem; Increment(ref end); size++; } else Resize(); } /// /// If the array is full (e.g. size = array.Length) then create a new, larger array and copy the data into it. /// public void Resize() { throw new Exception("Queue Resize is not implemented - you should implement it"); }
///
/// Increments the value and handles the "gap of death" /// /// private void Increment(ref int value) { if (++value == myArray.Length) value = 0; }
///
/// Used by foreach /// /// System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }
public IEnumerator GetEnumerator() { int position = front; for (int i = 0; i < size; i++) { yield return myArray[position]; Increment(ref position); } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
