#include<stdio.h>
#include<memory.h>
#include<iostream>
using namespace std;
template<typename T>
class MyDequeue{
private:
T *arr = new T[Capacity];
size_t ArrSize;
size_t Capacity;
int frontptr;
int rearptr;
void resize_arr(size_t new_capacity){
T *arr1 = new T[new_capacity];
for(int i = 0; i<ArrSize; i++){
arr1[i] = arr[(frontptr + i)%Capacity];
}
delete [] arr;
arr = arr1;
frontptr = 0;
rearptr = ArrSize;
Capacity = new_capacity;
}
public:
MyDequeue(): ArrSize(0),frontptr(0), rearptr(0), Capacity(0), arr(nullptr){}
T& operator[](int index){
if(ArrSize == 0 || Capacity == 0){
perror("Index out of bounds");
static T dum;
return dum;
}
if(index > ArrSize-1 || index < -ArrSize + 1){
perror("Index out of bounds");
static T dum;
return dum;
}
if(index < 0){
index += ArrSize;
}
return arr[(frontptr + index)% Capacity];
}
MyDequeue(size_t n): frontptr(0), rearptr(n-1), Capacity(n), ArrSize(n) {
arr =new T[Capacity];
for(int i = 0; i<ArrSize; i++){
arr[i] = T();
}
}
MyDequeue(size_t n, T x): frontptr(0), rearptr(n-1), Capacity(n), ArrSize(n){
arr = new T[Capacity];
for(int i= 0; i<ArrSize; i++){
arr[i] = x;
}
}
void push_front(T x){
if(ArrSize == Capacity){
resize_arr(Capacity == 0? 1: 2*Capacity);
}
frontptr = (frontptr - 1 + Capacity)%Capacity;
arr[frontptr] = x;
ArrSize++;
}
void push_back(T x){
if(ArrSize == Capacity){
resize_arr(Capacity==0?1:2*Capacity);
}
arr[rearptr] = x;
rearptr = (rearptr + 1)%Capacity;
ArrSize++;
}
void pop_front(){
if(ArrSize == 0){
perror("Underflow");
return;
}
frontptr = (frontptr + 1)%Capacity;
ArrSize--;
}
void pop_back(){
if(ArrSize == 0){
perror("Underflow");
return;
}
rearptr = (rearptr - 1 + Capacity)%Capacity;
ArrSize--;
}
T front(){
if(ArrSize == 0){
return T();
}
return arr[frontptr];
}
T back(){
if(ArrSize == 0){
return T();
}
return arr[rearptr];
}
bool empty(){
return ArrSize == 0;
}
int size(){
return ArrSize;
}
void resize(size_t n){
if(Capacity < n){
resize_arr(n);
}
if(ArrSize > n){
ArrSize = n;
}else{
for(size_t i = ArrSize; i<n; i++)
push_back(T());
}
// rearptr = (rearptr + n%ArrSize)%ArrSize;
// // int i = 0;
// while(i<ArrSize){
// arr1[i]= arr[i];
// i++;
// }
// delete [] arr;
// arr = arr1;
}
void clear(){
delete [] arr;
ArrSize = 0;
frontptr = 0;
rearptr = 0;
Capacity = 0;
}
int capacity(){
return Capacity;
}
};
int main(){
MyDequeue<int> a;
a.push_front(30);
// cout<<a.size()<<" ";
// a.pop_front();
// cout<<a.size();
// // for(int i = 0; i<a.size(); i++){
// cout<<a[i]<<"\n";
// }
//cout<<a[0]<<" ";
// cout<<a.size()<<" ";
// cout<<a[0]<<" ";
// a.pop_back();
// cout<<a[0];
for(int i= 0; i<20; i++){
a.push_back(i);
}
for(int i= 0; i<a.size(); i++){
cout<<a[i]<<" ";
}
cout<<a.front();
return 0;
}