struct DequeList {
struct Node {
explicit Node( int v ) : val(v) {}
Node( int v, Node* n, Node* p ) : val(v), next(n), prev(p) {}
int val ;
Node* next = nullptr ;
Node* prev = nullptr ;
};
Node* front = nullptr ;
Node* back = nullptr ;
std::size_t currentSize = 0 ;
DequeList() = default ;
// ...
// void insertionAtFront(int val) {
void insertAtFront(int val) {
if( currentSize == 0 ) { // empty list
// empty list: this becomes the only node in (both front and back of ) the list
// the next and prev of this only node are null
front = back = new Node(val) ;
}
else { // the list is not empty
// the new node added would be the prev of the node currently at the front
// the next of this new node is the current front of the list
// and the prev of this new node is null
front->prev = new Node( val, front, nullptr ) ;
front = front->prev ; // the front of the list is now the new node that was added
}
++currentSize ; // in either case, the current size goes up by one
}
};