Question
Hi there I need help finding an error in my project. I've finished pretty much all of the code but my code is stuck on
Hi there I need help finding an error in my project. I've finished pretty much all of the code but my code is stuck on this infinite loop and I can't seem to figure out what's wrong please help. Also please dont make any changes to the main.cpp only the minideque.h thank you!
//
// minideque.h
// minidequeproject
//
#ifndef minideque_h
#define minideque_h
#include
#include
#include
#include
#include
template <typename T>
class minideque {
private:
std::vector
std::vector
public:
minideque() = default; // do not change, C++ defaults are ok
minideque(const minideque
minideque& operator=(const minideque
~minideque() = default;
size_t size() const{ // TODO
return fronthalf_.size() + backhalf_.size();
};
size_t fronthalfsize() const{ //TODO
return fronthalf_.size();
};
size_t backhalfsize() const{ //TODO
return backhalf_.size();
};
bool empty() const{ //TODO
return (size()==0);
};
// 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(){
if (!fronthalf_.empty())
fronthalf_.pop_back();
else{
int m = backhalf_.size()/2;
for (int i = m; i <= 0; i--){
fronthalf_.push_back(backhalf_[i]);
backhalf_.erase(backhalf_.begin());
}
}
}; // TODO hardest (if)
void pop_back(){
// if (!backhalf_.empty())
// backhalf_.pop_back();
// else{
// int m = fronthalf.size()/2;
// for (int i = m; i < 0; i--){
// backhalf_.push_back(fronthalf_.begin());
};
void push_front(const T& value){ //TODO
fronthalf_.push_back(value);
};
void push_back(const T& value){
backhalf_.push_back(value);
}; // TODO
const T& front() const{
if(!fronthalf_.empty()){
return fronthalf_.back();
}
else
return backhalf_.front();
}; // TODO
const T& back() const{
if (!backhalf_.empty()){
return backhalf_.back();
}
else
return backhalf_.front();
}; // TODO
T& front(){ // TODO
if(!fronthalf_.empty()){
return fronthalf_.back();
}
else
return backhalf_.front();
};
T& back(){ //TODO
if (!backhalf_.empty()){
return backhalf_.back();
}
else
return backhalf_.front();
};
const T& operator[](size_t index) const{ //TODO
if(index return fronthalf_[fronthalf_.size()-(index+1)]; else return backhalf_[index-fronthalf_.size()]; }; T& operator[](size_t index){ if(index return fronthalf_[fronthalf_.size() - (index+1)]; else return backhalf_[index-fronthalf_.size()]; }; // TODO void clear(){ fronthalf_.clear(); backhalf_.clear(); }; // TODO friend std::ostream& operator<<(std::ostream& os, const minideque 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 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 cit != backhalf_.cend(); ++cit) { os << *cit << " "; } } }; #endif /* minideque_h */ ----------------------------------------------------------------------------------------------------------- // // minideque_project_main.cpp // minideque_project // // #include #include "minideque.h" template <typename T> bool testAnswer(const std::string& nameOfTest, const T& received, const T& expected); size_t testsPassed = 0; size_t testsFailed = 0; void test_minideque() { minideque<int> dq; 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<int> valuesfront = { 2, 3, 4, 5, 6, 7, 8 }; std::vector<int> valuesback = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }; 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 <typename T> 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; }
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