Question: Complete the implementation of class minideque , adding public/private member variables and functions as needed. Your code is tested in the provided main.cpp. Source Code
Complete the implementation of class minideque
Source Code Files
You are given skeleton code files with declarations that may be incomplete and without any implementation. Implement the code and ensure that all the tests in main.cpp pass successfully.
minideque.h: This is to be completed
main.cpp: This calls a test function (in minideque.h) that tests the output of your minideque
Main.cpp
#include
#include "minideque.h"
template
bool testAnswer(const std::string& nameOfTest, const T& received, const T& expected);
size_t testsPassed = 0;
size_t testsFailed = 0;
void test_minideque() {
minideque
dq.push_back(9);
testAnswer("dq[0] == dq.front()", dq[0] == dq.front(), true);
testAnswer("dq[0] == 9", dq[0], 9);
dq.push_front(1);
testAnswer("dq.front() == 1", dq.front(), 1);
testAnswer("dq.back() == 9", dq.back(), 9);
std::vector
std::vector
for (auto el : valuesfront) {
dq.push_front(el);
testAnswer("dq.front() == dq.push_front(el)", dq.front(), el);
}
for (auto el : valuesback) {
dq.push_back(el);
testAnswer("dq.back() == dq.push_back(el)", dq.back(), el);
}
int value = 9999;
dq[0] = value;
testAnswer("assign to array index", dq[0], value);
testAnswer("read from array index", dq[0], value);
std::cout << " clearing the minideque... ";
std::cout << "NOTE: the minideque keeps REBALANCING the front/back to have similar # entries ";
while (!dq.empty()) {
dq.pop_front();
std::cout << "dq.pop_front() ==> " << dq << " ";
if (!dq.empty()) {
if (dq.front() == 10 || dq.front() == 16) {
testAnswer("rebalancing fronthalf and backhalf vectors",
dq.fronthalfsize(), dq.backhalfsize());
}
}
}
std::cout << " Passed: " << testsPassed << " out of: " << testsPassed + testsFailed
<< " total tests. ";
}
template
bool testAnswer(const std::string& nameOfTest, const T& received, const T& expected) {
if (received == expected) {
std::cout << "PASSED " << nameOfTest
<< ": expected and received " << received << std::endl;
++testsPassed;
return true;
}
std::cout << "FAILED " << nameOfTest
<< ": expected " << expected << " but received " << received << std::endl;
++testsFailed;
return false;
}
int main(int argc, const char * argv[]) {
test_minideque();
std::cout << " \t\t...done. ";
return 0;
}
Minideque.h
#ifndef minideque_h
#define minideque_h
#include
#include
#include
#include
#include
template
class minideque { private:
std::vectorfronthalf_;
std::vectorbackhalf_;
public:
minideque() = default; // do not change, C++ defaults are ok
minideque(const minideque& other) = default; // rule of three
minideque& operator=(const minideque& other) = default;
~minideque() = default;
size_t size() const; // TODO
size_t fronthalfsize() const; // TODO
size_t backhalfsize() const; // TODO
bool empty() const; // TODO
// balance queue across both vectors if pop_front/back is requested on an empty vector
// e.g., minideque has this: | ABCDEFG
// after pop_front BCD | EFG (A discarded)
// symmetric logic for converse case: ABCDEFG | ===> ABC | DEF (G discarded) after pop_back
void pop_front(); // TODO
void pop_back(); // TODO
void push_front(const T& value); // TODO
void push_back(const T& value); // TODO
const T& front() const; // TODO
const T& back() const; // TODO
T& front(); // TODO
T& back(); // TODO
const T& operator[](size_t index) const; // TODO
T& operator[](size_t index); // TODO
void clear(); // TODO
friend std::ostream& operator<<(std::ostream& os, const minideque& dq) { // do not change
if (dq.empty()) { return os << "minideque is empty"; } dq.printFronthalf(os);
os << "| ";
dq.printBackhalf(os);
os << ", front:" << dq.front() << ", back:" << dq.back() << ", size:" << dq.size();
return os;
}
void printFronthalf(std::ostream& os=std::cout) const { // do not change if (empty()) { std::cout << "fronthalf vector is empty"; } for (typename std::vector::const_reverse_iterator crit = fronthalf_.crbegin();
crit != fronthalf_.crend(); ++crit) { os << *crit << " ";
}
}
void printBackhalf(std::ostream& os=std::cout) const { // do not change if (empty()) { os << "backhalf vector is empty"; } for (typename std::vector::const_iterator cit = backhalf_.cbegin();
cit != backhalf_.cend(); ++cit) { os << *cit << " ";
}
}
};
#endif /* minideque_h */
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
