I tried to implement a queue which stores it's elements in a resizing array. Seems to work fine. Can you tell me if i did something wrong?
public class Queue<T>
{
private T[] array;
public Queue()
{
array = new T[16];
}
private void Enqueue(T item)
{
if(Count == array.Length/2)
Grow();
array[Count++] = item;
}
public void Grow()
{
var temp = array;
array = new T[array.Length * 2];
Array.Copy(temp,startingPos,array,0,Count);
startingPos = 0;
}
public void Shrink()
{
var temp = array;
array = new T[array.Length / 2];
Array.Copy(temp,startingPos,array,0,Count);
startingPos = 0;
}
public T Dequeue()
{
if(Count == array.Length/4)
Shrink();
var item = array[startingPos];
array[startingPos++] = default(T);
Count--;
return item;
}
int startingPos = 0;
public int Count {get;set;}
}
System.Collections.Generic.Queue
does this. \$\endgroup\$