Will someone please check this and tell me if I did this problem correctly. The question is presented bold; I also need help with push. I couldn't figure out how to complete the definition or routine for it. I am not very good at big o notation to determine if the routines I wrote are O(1). Thanks in advance!!
A deque is a data structure consisting of a list of items, on which the following operations are possible:
push(x,d): Insert item x on the front end of deque d.
dq.push_front(x, d);
pop(d): Remove the front item from deque d and return it.
void Pop(Deque& d)
{
n = new Node;
n->data = val;
if (d.size == 0)
{
d.front = d.rear = n;
n -> prev = n -> next = NULL;
}
else {
n-> next = NULL;
n->prev = d.rear;
d.rear->next = n;
d.rear = n;
}
d.size--;
}
inject(x,d): Insert item x on the rear end of deque d.
void Inject(int val, Deque& d)
{
n = new Node;
n->data = val;
if (d.size == 0)
{
d.front = d.rear = n;
n -> prev = n -> next = NULL;
}
else
{
n-> next = NULL;
n->prev = d.rear;
d.rear->next = n;
d.rear = n;
}
d.size++;
}
eject(d): Remove the rear item from deque d and return it.
void Eject(Deque& d)
{
x->data = val;
if (d.size == 0)
{
d.front = d.rear = x;
x -> prev = x -> next = NULL;
}
else
{
x-> next = NULL;
x->prev = d.rear;
d.rear->next = x;
d.rear = x;
}
d.size--;
}
Write definitions and routines to support the deque that take O(1) time per operation.