Deque Flashcards

1
Q

Deque pushBack

A
    void pushBack(T value)
    {
        if (isFull())
        {
            cout << "Deque is full." << endl;
            return;
        }
        else if (isEmpty())
        {
            frontIndex = 0;
            backIndex = 0;
        }
        else
        {
            backIndex = (backIndex + 1) % MAXSIZE;
        }
        deque[backIndex] = value;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Deque pushFront

A
    void pushFront(T value)
    {
        if (isFull())
        {
            cout << "Deque is full" << endl;
            return;
        }
        else if (isEmpty())
        {
            frontIndex = 0;
            backIndex = 0;
        }
        else
        {
            frontIndex = (frontIndex - 1 + MAXSIZE) % MAXSIZE;
        }
        deque[frontIndex] = value;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Deque popFront

A
    void popFront()
    {
        if (isEmpty())
        {
            cout << "Deque is empty." << endl;
            return;
        }
        else if (frontIndex == backIndex)
        {
            frontIndex = -1;
            backIndex = -1;
        }
        else
        {
            frontIndex = (frontIndex + 1) % MAXSIZE;
        }
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Deque popBack

A
    void popBack()
    {
        if (isEmpty())
        {
            cout << "Deque is empty
        " << endl;
            return;
        }
        else if (frontIndex == backIndex)
        {
            frontIndex = -1;
            backIndex = -1;
        }
        else
        {
            backIndex = (backIndex - 1 + MAXSIZE) % MAXSIZE;
        }
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Deque peekFront

A
    T front() const
    {
        if (isEmpty())
        {
            cout << "Deque is empty." << endl;
            return T();
        }
        return deque[frontIndex];
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Deque peekBack

A
    T back() const
    {
        if (isEmpty())
        {
            cout << "Deque is empty." << endl;
            return T();
        }
        return deque[backIndex];
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Deque isEmpty

A
    bool isEmpty() const
    {
        return frontIndex == -1;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Deque isFull

A
    bool isFull() const
    {
        return (backIndex + 1) % MAXSIZE == frontIndex;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Deque clear

A
    void clear()
    {
        frontIndex = -1;
        backIndex = -1;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Deque display

A
template <typename T>
void display(const Deque<T>& d)
{
    if (d.isEmpty())
    {
        cout << "Deque is empty" << endl;
        return;
    }

    Deque<T> temp = d;
    while (!temp.isEmpty())
    {
        cout << temp.front() << " ";
        temp.popFront();
    }
    cout << endl;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Deque reverse

A
template <typename T>
void reverse(Deque<T>& d)
{
    if (d.isEmpty())
    {
        cout << "Deque is empty" << endl;
        return;
    }

    stack<T> s;
    while (!d.isEmpty())
    {
        s.push(d.front());
        d.popFront();
    }

    while (!s.empty())
    {
        d.pushBack(s.top());
        s.pop();
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly