Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm not sure I am on the right track with this assignment. Assignent: Write a template queue class as defined below: private data member: a

I'm not sure I am on the right track with this assignment.

Assignent:

Write a template queue class as defined below:

private data member: a STL list

public member functions:

-empty

-size

-enqueue

-deque

-front

-back

Then write a driver to test the above queue class.

My code:

Header.h

#include #include #include using namespace std; template class Queue { private: int Front, Back, count; list QList; public: Queue() { Front = 0; Back = 0; count = 0; }

bool empty() { return QList.empty(); }

int size() { QList.size(); }

bool enqueue(const T & x) { list.push_back(x); rear = rear + 1; count = count + 1; return true; }

bool dequeue(const T & x) { if (!empty()) { QList.pop_front(); count--; Front = Front + 1; return true; } return false; } typedef int QueueElement front() const { return QList.front(); }

typedef int QueueElement back() const { return QList.back(); } };

Source.cpp

#include "Header.h" #include #include using namespace std; template void ShowCommands() { cout << "Use the following commands to test the Queue class:" << endl; cout << "a---add an element to the queue" << endl; cout << "d---display contents of queue" << endl; cout << "e---test whether a queue is empty" << endl; cout << "f---retrieve the item at the front of the queue" << endl; cout << "r---remove item from front of the queue" << endl; cout << "s---display the size of queue" << endl; cout << "b---retrieve the item at the back of the queue" << endl; cout << "q---quit testing" << endl; } template int main() { typedef int QueueElement item; char command; list q; ShowCommands(); do { cout << "Command? "; cin >> command; if (isupper(command)) command = tolower(command); switch (command) { case 'a': cout << "Enter item to add to queue: "; cin >> item; q.enqueue(item); cout << "--> " << item << " added "; break; case 'd': cout << "--> Queue contents: " << q << endl; break; case 'e': cout << "--> Queue " << (q.empty() ? "is" : "is not") << " empty "; break; case 'f': cout << "--> " << q.front() << " is at the front "; break; case 'r': q.dequeue(); cout << "--> Front element removed "; break; case 's': cout << "--> the size of the queue is: " << q.size() << endl; break; case 'b': cout << "--> " << q.back() << " is at the back "; break; case 'q': cout << "--> End of test "; break; default: cout << "Illegal command: " << command << endl; } } while (command != 'q'); }

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

Advanced MySQL 8 Discover The Full Potential Of MySQL And Ensure High Performance Of Your Database

Authors: Eric Vanier ,Birju Shah ,Tejaswi Malepati

1st Edition

1788834445, 978-1788834445

More Books

Students also viewed these Databases questions