// our task is to create a vector now
#include<bits/stdc++.h>
using namespace std;
class Node {
public:
int value;
Node* next;
Node* prev;
Node() {
value = 0;
next = NULL;
prev = NULL;
}
Node(int value) {
this->value = value;
next = NULL;
prev = NULL;
}
void setNext(Node* next) {
this->next = next;
}
void setPrev(Node* prev) {
this->prev = prev;
}
void setValue(int value) {
this->value = value;
}
};
class Vector {
public:
Node* head;
Node* tail;
Vector() {
head = new Node();
tail = head;
}
// tail = 'banani'
// newNode = 'shewrapara'
// tail-next = newnode = 'shewrapara'
// newPrev->prev = tail = 'banani'
// tail = 'shewrapara'
void push_back(int value) {
Node* newNode = new Node(value);
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
void pop_back() {
if(tail == head) {
cerr << "vector is empty, so your can't pop" << endl;
return; // vector is empty so nothing to pop
}
tail = tail->prev;
tail->next = NULL;
}
int back() {
if(tail == head) {
cerr << "vector is empty, so your can't pop" << endl;
return -1;
}
return tail->value;
}
int front() {
if(head->next == NULL) {
cerr << "vector is empty, so your can't pop" << endl;
return -1;
}
return head->next->value;
}
void push_front(int value)
{
Node* newNode = new Node(value);
newNode->next = head->next;
newNode->prev = head;
head->next = newNode;
}
void pop_front()
{
if(head->next == NULL){
cerr << "Vector is empty so you cannot pop front" << endl;
return;
}
Node* del_node = head->next;
head->next = del_node->next;
if(del_node->next != NULL)
{
del_node->next->prev = head;
}
}
};
int main() {
Vector v;
v.push_back(5);
cout << v.front() << endl;
v.pop_front();
cout << v.front() << endl;
v.pop_front();
v.push_front(10101);
cout << v.front() << endl;
}