Deque own Implementation push_front

Viewed 22

I have a task to implement deque but by writing it myself. push_back is working good but I have a problem with push_front which is not working correctly I checked that based on the deque library. Could you please help and explain ?

    void push_back(int x) {
    if (capacity == elements) {
        int *tmp = new int[capacity * 2];

        for(int i=0;i<capacity;i++){   
            tmp[i]=mem[i];
        }

        delete [] mem;
        mem = tmp;
        capacity *=2;  

        first =mem[0];   


    }

    int insertion_location = (first + elements) % capacity;

    mem[insertion_location] = x;

    elements++;


}

void push_front(int x){
    if (capacity == elements) {
        int *tmp = new int[capacity * 2];

        for(int i=0;i<capacity;i++){   
            tmp[i]=mem[i];
        }

        delete [] mem;
        mem = tmp;
        capacity *=2;  

        first =mem[0];   


    }

    int insertion_location = (elements - first) % capacity;

    mem[insertion_location] = x;

    elements++;

}
0 Answers
Related