Not sure how to implement a deque
This is what I have so far but I know my push_back and front functions need work. Just want a little guidance here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <cassert>
#include <vector>
#include <iostream>
using namespace std;
class Deque{
private:
vector<int> a;//push_front
vector<int> b;//push_back
int indexA = -1;
int indexB = -1;
public:
Deque();
int front();
int back();
void push_back(int x);
void push_front(int x);
void pop_back();
void pop_front();
};
Deque::Deque()
{
}
void Deque::push_back(int x)
{
b.push_back(x);
indexB++;
}
void Deque::push_front(int x)
{
a.push_back(x);
indexA++;
}
void Deque::pop_back()
{
}
int Deque::front()
{
if(a.empty())
{
return b.front();
}
else
{
return a.back();
}
}
int Deque::back()
{
if(b.empty())
{
return a.front();
}
else
{
return b.back();
}
}
int main()
{
Deque d;
d.push_back(10);
d.push_back(20);
assert(d.front() == 10);
assert(d.back() == 20);
d.push_front(1);
d.push_front(2);
d.push_front(3);
assert(d.front() == 3);
assert(d.back() == 20);
/*
d.pop_back();
d.pop_back();
d.pop_back();
assert(d.front() == 3);
assert(d.back() == 2);
d.push_back(1);
d.push_back(0);
*/
cout << "SUCCESS\n";
}
|
Last edited on
You're not asking any questions.
I am curious why you are implementing your Deque using std::vector.
As an exercise it might be better to implement it ground up?
Andy
Topic archived. No new replies allowed.