Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Complete the implemention of class Deque below. The test codes Deque_test.cpp and test.cpp should compile and run with your implementation without any warnings or errors.
Complete the implemention of class Deque below.
The test codes Deque_test.cpp and test.cpp should compile and run with your implementation without any warnings or errors.
Do not use STL Deque.
#ifndef DEQUE_H #define DEQUE_H // Deque.h - an implementation of double-ended queue #includeusing namespace std; template class DequeIterator; template class Deque { public: typedef DequeIterator iterator; Deque(): vecOne(), vecTwo() { } Deque(const unsigned int size, const T& initial): vecOne(size/2, initial), vecTwo(size-(size/2), initial) { } Deque(const Deque & d): vecOne(d.vecOne), vecTwo(d.vecTwo) { } ~Deque() { } // destructors for vecOne and vecTwo are automatically called // never call a destructor explicitly Deque & operator=(const Deque & d); T & operator[](unsigned int); T & front(); T & back(); bool empty(); iterator begin(); iterator end(); void erase(const iterator &); void erase(const iterator &, const iterator &); void insert(const iterator &, const T &); int size(); void push_front(const T & value); void push_back(const T & value); void pop_front(); void pop_back(); protected: vector vecOne; vector vecTwo; }; // Your code goes here ... template class DequeIterator { friend class Deque ; typedef DequeIterator iterator; public: DequeIterator(): theDeque(0), index(0) { } DequeIterator(Deque * d, int i): theDeque(d), index(i) { } DequeIterator(const iterator & d): theDeque(d.theDeque), index(d.index) { } T & operator*(); iterator & operator++(); iterator operator++(int); iterator & operator--(); iterator operator--(int); bool operator==(const iterator & r); bool operator!=(const iterator & r); bool operator<(const iterator & r); T & operator[](unsigned int i); iterator operator=(const iterator & r); iterator operator+(int i); iterator operator-(int i); protected: Deque * theDeque; int index; }; #endif
// Deque_test.cpp #include#include #include "Deque.h" using namespace std; 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); Deque ::iterator i; int counter = 3; for (i = d.begin(); i != d.end(); i++) assert(*i == counter--); for (counter = 0; counter < d.size(); counter++) assert(d[counter] == d.size()-counter-1); i = d.begin() + 3; Deque ::iterator j(i), k; k = j = i - 2; assert(*k == 2); for (i = d.begin(); not(i == d.end()); ++i) cout << *i << " "; cout << endl; d.erase(d.begin()+3); d.erase(d.begin(), d.begin()+2); assert(d.size() == 1); assert(d[0] == 1); Deque c(d); c.front() = 3; assert(c.back() == 3); c.push_front(1); c.insert(c.begin(), 0); c.insert(c.begin()+2, 2); for (i = c.begin(); not(i == c.end()); ++i) cout << *i << " "; cout << endl; for (counter = 0; counter < c.size(); counter++) assert(c[counter] == counter); cout << "SUCCESS "; }
// test.cpp #include#include //#include #include "Deque.h" using namespace std; main() { Deque d; d.push_back(10); d.push_back(20); d.push_front(1); d.push_front(2); d.push_front(3); Deque c; c = d; Deque ::iterator i; for (i = c.begin(); i != c.end(); ++i) cout << *i << " "; cout << endl; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started