template<typename T>
class Deque {
public:
explicit Deque(int capacity);
void PushFront(const T &node);
void PushBack(const T &node);
T PopFront();
T PopBack();
T Front() { return data_[head_]; }
T Back() { return data_[(tail_ - 1 + capacity_) % capacity_]; }
bool IsNotEmpty() { return head_ != tail_; };
bool IsEmpty() { return head_ == tail_; }
bool IsFull() { return (tail_ + 1) % capacity_ == head_; };
int Size();
int Head() { return head_; }
int Tail() { return tail_; }
public:
int capacity_, head_, tail_;
T *data_;
};
template<typename T>
Deque<T>::Deque(int capacity) {
capacity_ = capacity;
head_ = tail_ = ;
data_ = new T[capacity_];
}
template<typename T>
void Deque<T>::PushBack(const T &node) {
if (IsFull()) {
std::__throw_logic_error("queue is full");
}
data_[tail_] = node;
tail_ = (tail_ + 1) % capacity_;
}
template<typename T>
void Deque<T>::PushFront(const T &node) {
if (IsFull()) {
std::__throw_logic_error("queue is full");
}
head_ = (head_ - 1 + capacity_) % capacity_;
data_[head_] = node;
}
template<typename T>
T Deque<T>::PopBack() {
if (IsEmpty()) {
std::__throw_logic_error("queue is empty");
}
tail_ = (tail_ - 1 + capacity_) % capacity_;
return data_[tail_];
}
template<typename T>
T Deque<T>::PopFront() {
if (IsEmpty()) {
std::__throw_logic_error("queue is empty");
}
head_ = (head_ + 1) % capacity_;
return data_[(head_ - 1 + capacity_) % capacity_];
}
template<typename T>
int Deque<T>::Size() {
if (head_ <= tail_) {
return tail_ - head_;
} else {
return tail_ + (capacity_ - head_);
}
}
int main() {
Deque<int> deq(10);
deq.PushBack(2);
deq.PushFront(1);
deq.PushFront();
deq.PushBack(3);
printf("head=%d tail=%d size=%d back=%d\n", deq.Head(), deq.Tail(), deq.Size(), deq.Back());
while (deq.Size() > 2) {
printf("%d\n", deq.PopBack());
}
deq.PushBack(2);
deq.PushFront(1);
deq.PushFront();
deq.PushBack(3);
printf("head=%d tail=%d size=%d front=%d\n", deq.Head(), deq.Tail(), deq.Size(), deq.Front());
while (deq.IsNotEmpty()) {
printf("%d\n", deq.PopFront());
}
return ;
}