Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I have a queue program that needs to be converted to a template. It's in C++ obviously and runs with no errors, just needs

Hi, I have a queue program that needs to be converted to a template. It's in C++ obviously and runs with no errors, just needs the conversions. Thank you for your time.

#include #include #include #include using namespace std;

ofstream outfile;

class list { private: static const size_t CAP = 65; typedef char vt; vt data[CAP]; size_t pos, used;

public: //Constructors list() { for (int i = 0; i < CAP; i++) data[i] = 0; pos = 0; used = 0; } list(const list& other) { for (int i = 0; i < CAP; i++) data[i] = other.data[i]; pos = other.pos; used = other.used; }

friend ostream& operator <<(ostream& out, const list& l);

bool empty() { if (used == 0) return true; else return false; }

void first() { pos = 0; }

void last() { pos = used - 1; }

void prev() { if (pos > 0) pos--; }

void next() { if (pos < used - 1) pos++; }

vt getPos() { return pos; }

void setPos(size_t val) { pos = val; }

void insertBefore(vt val) {

if (size() < CAP) { if (used == 0) { data[used] = val; used++; pos = 0; } else if (pos == 0) { for (int k = used; k > pos; k--) data[k] = data[k - 1]; data[pos] = val; used++; } else if (used <= pos) { data[used] = val; used++; pos = used - 1; } else if (pos != 0) { for (int i = used; i > (pos - 1); i--) data[i] = data[i - 1]; data[pos] = val; used++; pos--; } } }

void insertAfter(vt val) {

if (used == 0) { data[used] = val; used++; pos = 0; } else if (used <= pos) { data[used] = val; used++; pos = used - 1; } else if (pos != CAP) { for (int i = used; i > (pos + 1); i--) data[i] = data[i - 1]; data[used] = val; used++; pos++; } }

vt getElement() { return data[pos]; }

vt size() { return used; }

void replace(vt val) { if (val > 0) data[pos] = val; }

void erase() { if (used == 0) return; for (int i = pos; i < used; i++) data[i] = data[i + 1]; used--; }

void clear() { for (int i = 0; i < CAP; i++) data[i] = 0; used = 0; pos = 0; }

int get_CAP() { return CAP; }

bool operator ==(const list& other) { int x = 0, i = 0;

for (i = 0; i < CAP; i++) { if (data[i] = other.data[i]) x++; }

if (i = 20) return true; else return false; }

void operator +(const list& other) { int x = 0, p = used; while (other.data[x] != 0 && used < 20) { data[p] = other.data[x]; x++; used++; } }

void operator =(const list& other) { for (int i = 0; i < CAP; i++) { data[i] = other.data[i]; } pos = other.pos; used = other.used; }

};

ostream& operator <<(ostream& out, const list& l) { if (l.used == 0) return out; for (int p = 0; p < l.used; p++) { if (p >= l.CAP) { out << endl; return out; } out << l.data[p] << " "; } return out; }

class queue { private: list myQ; public: bool empty() { return myQ.empty(); }

bool full() { return (myQ.size() == myQ.get_CAP()); }

void enqueue(char v) { myQ.last(); myQ.insertAfter(v); }

void dequeue(int x) { if (x + myQ.getPos() > myQ.size()) { while (x - 1 > 0) { if (myQ.getPos() + 1 == myQ.size()) { myQ.first(); } else { myQ.next(); } x--; } myQ.erase(); } else { while (x - 1 > 0) { myQ.next(); x--; } if (myQ.getPos() + 1 == myQ.size()) { myQ.erase(); myQ.first(); } else { myQ.erase(); } } }

void start() { myQ.first(); }

char top() { return myQ.getElement(); }

int size() { return myQ.size(); } };

int main() { outfile.open("Suitor Results.txt");

queue Q; char charX = '#';

Q.enqueue(charX); for (int i = 1; i < 65; i++) { charX++; Q.enqueue(charX); }

Q.start();

while (Q.size() != 1) { Q.dequeue(4); }

Q.start(); outfile << "The winner is: " << Q.top() << endl;

outfile.close(); system("Pause"); return EXIT_SUCCESS; }

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

Oracle 10g SQL

Authors: Joan Casteel, Lannes Morris Murphy

1st Edition

141883629X, 9781418836290

More Books

Students also viewed these Databases questions

Question

8. Describe the steps in the development planning process.

Answered: 1 week ago