public static void Main()
RingBuffer<int> testBuf = new RingBuffer<int>(5);
while(testBuf.Count > 0) {
Console.WriteLine(testBuf.PopBack());
Console.WriteLine("=============");
while(testBuf.Count > 0) {
Console.WriteLine(testBuf.PopFront());
Console.WriteLine("=============");
while(testBuf.Count > 0) {
Console.WriteLine(testBuf.PopBack());
Console.WriteLine("=============");
public class RingBuffer<T> {
public RingBuffer(int capacity) {
public void PushFront2(T val) {
if(FrontIndex != count-1){
Array[FrontIndex+1] = val;
public void PushFront(T val) {
if(FrontIndex == MaxCap) FrontIndex = 0;
if(FrontIndex == BackIndex) BackIndex++;
if(BackIndex == MaxCap || BackIndex == -1) BackIndex = 0;
if(count < MaxCap) count ++;
public void PushBack(T val) {
if(FrontIndex == BackIndex)
throw new System.Exception("Can't access an empty array");
return Array[FrontIndex];
throw new System.Exception("Can't access an empty array");
return Array[FrontIndex+1];
throw new System.Exception("Can't access an empty array");
T val = Array[BackIndex];
if(FrontIndex==BackIndex) {
if(BackIndex >= MaxCap) BackIndex = 0;
public void PushBack2(T val) {
BackIndex = FrontIndex+1;
throw new System.Exception("Can't access an empty array");
throw new System.IndexOutOfRangeException();