#include <iostream>
using namespace std;
template <class T>class Deque {
protected:
struct node {
T data;
node* next;
};
node* head;
node* tail;
size_t len;
public:
Deque() {
head = NULL;
tail = NULL;
len = 0;
}
~Deque() {
node* ptr = NULL;
while (head != NULL) {
ptr = head->next;
delete head;
head = ptr;
}
}
void push_back(T var) {
node* ptr = new node;
ptr->data = var;
ptr->next = NULL;
if (tail != NULL) {
tail->next = ptr;
}
else {
head = ptr;
}
tail = ptr;
len++;
}
void push_front(T var) {
node* ptr = new node;
ptr->data = var;
ptr->next = head->next;
head = ptr;
if (tail == NULL) {
tail = ptr;
}
len++;
}
void pop_front() {
if (len <= 0) {
abort();
}
node* ptr = tail->next;
delete tail;
tail = ptr;
if (tail == NULL) {
head = NULL;
}
len--;
}
void pop_back() {
if (len <= 0) {
abort();
}
node* ptr = head->next;
delete head;
head = ptr;
if (head == NULL) {
tail = NULL;
}
len--;
}
T front() {
return head->data;
}
T back() {
return tail->data;
}
bool empty() {
return head == NULL && tail == NULL;
}
size_t size() {
return len;
}
void clear() {
while (len > 0) {
pop_back();
pop_front();
}
}
};