http://kid5.tistory.com/261


#pragma once

#include <iostream>

using namespace std;


template <class T, size_t SIZE>

class CircularDeque

{

public:

CircularDeque()

{

front = rear = 0;

}

void push_back(T d)

{

if (isFull()) throw;


data[rear] = d;

rear = (rear + 1) % SIZE;

}

void pop_front()

{

if (isEmpty()) throw;

front = (front + 1) % SIZE;

}

void push_front(T d)

{

if (isFull()) throw;


front = (front - 1 + SIZE) % SIZE;

data[front] = d;

}

void pop_back()

{

if (isEmpty()) throw;

rear = (rear - 1 + SIZE) % SIZE;

}


T Front()

{

if (isEmpty()) throw;

return data[front % SIZE];

}

T Back()

{

if (isEmpty()) throw;

return data[rear - 1 % SIZE];

}

size_t size()

{

return front <= rear ? rear - front : (rear + SIZE) - front;

}

void display()

{

cout << "data in the Deque : \n";

for (size_t i = 0, len = size(); i < len; ++i)

{

cout << data[(front + i) % SIZE] << endl;

}

}

bool isEmpty()

{

return front == rear;

}

bool isFull()

{

return front == (rear + 1) % SIZE;

}

private:

int front;

int rear;

T data[SIZE];

};