In: Computer Science
Add a method to OurQueue class that dequeues the Nth item on a queue and returns it. It must remove the Nth item from the Queue but leave the rest of the queue.
Test it with the following code:
Console.WriteLine("Testing DequeueNth"); OurQueue<string> ourQ = new OurQueue<string>(12); // Empty Q try { ourQ.DequeueNth(0); Console.WriteLine("\a Error on empty list"); } catch { Console.WriteLine("Empty Queue worked"); } for (int i = 0; i < 9; ++i) ourQ.Enqueue("a" + i); for (int i = 0; i < 7; ++i) ourQ.Dequeue(); for (int i = 0; i < 5; ++i) ourQ.Enqueue("b" + i); Console.WriteLine(ourQ); Console.WriteLine("Deleted {0}", ourQ.DequeueNth(5)); Console.WriteLine(ourQ); Console.WriteLine("Deleted {0}", ourQ.DequeueNth(4)); Console.WriteLine(ourQ);
public class ListQueue<T> : List<T>
{
new public void Add(T item) { throw new NotSupportedException();
}
new public void AddRange(IEnumerable<T> collection) { throw
new NotSupportedException(); }
new public void Insert(int index, T item) { throw new
NotSupportedException(); }
new public void InsertRange(int index, IEnumerable<T>
collection) { throw new NotSupportedException(); }
new public void Reverse() { throw new NotSupportedException();
}
new public void Reverse(int index, int count) { throw new
NotSupportedException(); }
new public void Sort() { throw new NotSupportedException(); }
new public void Sort(Comparison<T> comparison) { throw new
NotSupportedException(); }
new public void Sort(IComparer<T> comparer) { throw new
NotSupportedException(); }
new public void Sort(int index, int count, IComparer<T>
comparer) { throw new NotSupportedException(); }
public void Enqueue(T item)
{
base.Add(item);
}
public T Dequeue()
{
var t = base[0];
base.RemoveAt(0);
return t;
}
public T Peek()
{
return base[0];
}
}