deque Question 9

Last Updated :
Discuss
Comments

What will be the output of the following program?

C++
#include <bits/stdc++.h>
using namespace std;
void specialInsert(deque<int> &dq, int val)
{
    if (dq.size() == 0)
        dq.push_back(val);
    else if (val > dq.back())
        dq.push_back(val);
    else if (val < dq.front())
        dq.push_front(val);
    else
    {
        dq.insert(dq.end() - 1, val);
    }
}
int main()
{
    deque<int> dq;
    specialInsert(dq, 1);
    specialInsert(dq, 9);
    specialInsert(dq, 7);
    specialInsert(dq, 2);
    cout << dq.front() << endl;
    cout << dq.back();
    return 0;
}


9 2

1 2

1 9

7 9

Share your thoughts in the comments