Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help with finishing Header file in C++. Here is the criteria You are given a partial implementation of a mini-deque class. minideque is a templated

Help with finishing Header file in C++.

Here is the criteria

You are given a partial implementation of a mini-deque class. minideque is a templated class that holds the deque. Unlike the actual std::deque class, minideque will NOT have the iterator classes (iterator, const_iterator, reverse_iterator, and const_reverse_iterator). This means you will also NOT have to provide the iterator-based functions: erase, begin and end.

HERE ARE THE TWO FILES DO NOT CHANGE MAIN.CPP

Minideque.h

//

// minideque.h

// minidequeproject

//

#ifndef minideque_h

#define minideque_h

#include

#include

#include

#include

#include

template

class minideque {

private:

std::vector fronthalf_;

std::vector backhalf_;

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 */

MAIN.CPP

//

// minideque_project_main.cpp

// minideque_project

//

//

#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;

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 valuesfront = { 2, 3, 4, 5, 6, 7, 8 };

std::vector 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

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke

12th Edition International Edition

1292023422, 978-1292023427

More Books

Students also viewed these Databases questions

Question

Explain the steps involved in training programmes.

Answered: 1 week ago

Question

What are the need and importance of training ?

Answered: 1 week ago

Question

What is job rotation ?

Answered: 1 week ago

Question

=+ Who is the negotiation partner at company level?

Answered: 1 week ago

Question

=+Which associations exist?

Answered: 1 week ago