Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ As you may recall, the implementation of the cs20::Queue < Object > data structure presented in class used a linked node structure with an

C++

As you may recall, the implementation of the cs20::Queue< Object > data structure presented in class used a linked node structure with an identified front and back node. Based on this data structure, write the following method: template bool Queue::separatedBy( Object a, Object b, int amount ) const; This method should determine whether the Queue has the two specified object arguments separated by the desired amount of QueueNodes. Feel free to define any public or private methods or members on the class Queueas you need. Please work with the code from class. For example, please consider the queue shown below:

separatedBy( 8, 20, 1 ) should return false because 8 and 20 are not found on the queue. separatedBy( 12, 20, 1 ) should return false because 20 is not found on the queue. separatedBy( 12, 1, 1 ) should return false because 12 and 1 are not separated by 1 QueueNode on the queue. separatedBy( 12, 1, 3 ) should return true because 12 and 1 are separated by 3 QueueNodes (namely, 5, 7, and 13) on the queue.

code is provided here

https://dropfile.to/QevBJfK access key; 2vs7ny6

https://dropfile.to/W7VaG8c access key: FaQ5K9G

////////////////////////////// CODE//////////////////////////////

//////////////////QueueMenu.cpp//////////////////////////////////////////////

#include

#include "Queue.h"

#include "EmptyQueue.h"

#include "QueueNode.h"

#include "Queue.cpp"

#include "QueueNode.cpp"

using namespace std;

using namespace cs20;

enum CHOICE {MAKEEMPTY, ENQUEUE, ISEMPTY, DEQUEUE, GETFRONT, QUIT, PRINT };

CHOICE menu();

int main(int argc, char* argv[]) {

int value;

Queue q;

CHOICE choice;

do {

choice = menu();

switch( choice ) {

case MAKEEMPTY:

q.makeEmpty();

break;

case ISEMPTY:

if (q.isEmpty()) {

cout << "queue is empty" << endl;

}

else {

cout << "queue is not empty" << endl;

}

break;

case DEQUEUE:

try {

value = q.dequeue();

cout << "Here's the value dequeued: ";

cout << value << endl;

} catch( EmptyQueue eq ) {

cout << "You silly... don't try dequeueing an empty queue!" << endl;

}

break;

case GETFRONT:

try {

value = q.getFront();

cout << "Here's the value returned from getFront: ";

cout << value << endl;

} catch( EmptyQueue eq ) {

cout << "You silly... don't try gettingFront an empty queue!" << endl;

}

break;

case ENQUEUE:

cout << "Please provide int to enqueue: ";

cin >> value;

q.enqueue( value );

break;

case PRINT:

q.printQueue( cout );

cout << endl;

break;

case QUIT:

break;

}

} while (choice != QUIT);

return( 0 );

}

CHOICE menu() {

char choice;

CHOICE result;

cout << "(M)akeEmpty I(s)Empty (E)nqueue (D)equeue (G)etFront (P)rint (Q)uit: " << endl;

cin >> choice;

switch( choice ) {

case 'E':

case 'e':

result = ENQUEUE;

break;

case 'M':

case 'm':

result = MAKEEMPTY;

break;

case 'S':

case 's':

result = ISEMPTY;

break;

case 'D':

case 'd':

result = DEQUEUE;

break;

case 'G':

case 'g':

result = GETFRONT;

break;

case 'Q':

case 'q':

result = QUIT;

break;

case 'P':

case 'p':

result = PRINT;

break;

}

return( result );

}

void sample() {

Queue q1;

Queue q2;

cout << "making q1" << endl;

q1.enqueue( 1 );

q1.enqueue( 2 );

cout << "print q1" << endl;

q1.printQueue( cout );

cout << endl;

cout << "q2 = q1" << endl;

q2 = q1;

cout << "print q2" << endl;

q2.printQueue( cout );

cout << endl;

q1.dequeue();

cout << "dequeue q1" << endl;

cout << "print q2" << endl;

q2.printQueue( cout );

cout << endl;

cout << "print q1" << endl;

q1.printQueue( cout );

cout << endl;

}

//////////////////Queue.h////////////////////////////////////////////////////////////////////////////////////////

#ifndef QUEUE_H

#define QUEUE_H

#include

#include "QueueNode.h"

#include "EmptyQueue.h"

namespace cs20 {

template

class Queue {

public:

Queue();

Queue( const Queue& rhs );

~Queue();

bool isEmpty() const;

void makeEmpty();

void enqueue( const Object& data );

const Object dequeue();

const Object& getFront() const;

const Queue& operator =( const Queue& rhs );

std::ostream& printQueue( std::ostream& outs );

private:

QueueNode * frontNode;

QueueNode * backNode;

};

}

#endif

//////////////////Queue.cpp////////////////////////////////////////////////////////////////////////////////////////

#ifndef QUEUE_CPP

#define QUEUE_CPP

#include "Queue.h"

namespace cs20 {

template

Queue::Queue() {

frontNode = nullptr;

backNode = nullptr;

}

template

Queue::Queue( const Queue& rhs ) {

frontNode = nullptr;

backNode = nullptr;

*this = rhs;

}

template

Queue::~Queue() {

makeEmpty();

// when empty, frontNode and backNode will already be null

}

template

bool Queue::isEmpty() const {

return( (frontNode == nullptr) );

}

template

void Queue::makeEmpty() {

while (!isEmpty()) {

dequeue();

}

}

template

void Queue::enqueue( const Object& data ) {

QueueNode* newNode = new QueueNode( data );

if (isEmpty()) {

frontNode = newNode;

backNode = newNode;

}

else {

backNode->setNext( newNode );

backNode = backNode->getNext();

}

}

template

const Object Queue::dequeue() {

Object frontData = getFront();

QueueNode * oldFront = frontNode;

frontNode = frontNode->getNext();

delete oldFront;

return( frontData );

}

template

const Object& Queue::getFront() const {

if (isEmpty()) {

throw EmptyQueue();

}

return( frontNode->getElement() );

}

// Deep copy of linked Queue

template

const Queue& Queue::operator =( const Queue& rhs ) {

if (this != &rhs) {

makeEmpty();

QueueNode * rhsFrontNode = rhs.frontNode;

while( rhsFrontNode != nullptr) {

enqueue( rhsFrontNode->getElement() );

rhsFrontNode = rhsFrontNode->getNext();

}

}

return( *this );

}

template

std::ostream& Queue::printQueue( std::ostream& outs ) {

if (isEmpty()) {

outs << "Empty Queue";

}

else {

outs << "FRONT: ";

QueueNode * node = frontNode;

while( node != nullptr) {

outs << node->getElement();

outs << " ";

node = node->getNext();

}

outs << ":BACK";

}

return( outs );

}

}

#endif

//////////////////QueueNode.h////////////////////////////////////////////////////////////////////////////////////////

#ifndef QUEUENODE_H

#define QUEUENODE_H

#include

namespace cs20 {

template

class QueueNode {

public:

QueueNode( const Object& theElement = Object(), QueueNode * node = nullptr );

// these accessors and mutators are called

// from Queue class

// no public methods expose QueueNode instances

// outside the Queue class

const Object& getElement() const;

QueueNode* getNext() const;

void setNext( QueueNode * node );

private:

Object element;

QueueNode* next;

};

}

#endif

//////////////////QueueNode.cpp////////////////////////////////////////////////////////////////////////////////////////

#ifndef QUEUENODE_CPP

#define QUEUENODE_CPP

#include "QueueNode.h"

namespace cs20 {

template

QueueNode::QueueNode( const Object& theElement,

QueueNode * node ) {

element = theElement;

next = node;

}

template

const Object& QueueNode::getElement() const {

return( element );

}

template

QueueNode* QueueNode::getNext() const {

return( next );

}

template

void QueueNode::setNext( QueueNode * node ) {

next = node;

}

}

#endif

//////////////////EmptyQueue.h////////////////////////////////////////////////////////////////////////////////////////

#ifndef EMPTYQUEUE_H

#define EMPTYQUEUE_H

#include

#include

namespace cs20 {

class EmptyQueue : public std::logic_error {

public:

EmptyQueue( const std::string& msg = "" );

};

}

#endif

//////////////////EmptyQueue.cpp////////////////////////////////////////////////////////////////////////////////////////

#include "EmptyQueue.h"

namespace cs20 {

EmptyQueue::EmptyQueue( const std::string& msg ) : std::logic_error( msg.c_str() ) {}

}

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_2

Step: 3

blur-text-image_3

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

Advances In Spatial And Temporal Databases 11th International Symposium Sstd 2009 Aalborg Denmark July 8 10 2009 Proceedings Lncs 5644

Authors: Nikos Mamoulis ,Thomas Seidl ,Kristian Torp ,Ira Assent

2009th Edition

3642029817, 978-3642029813

More Books

Students also viewed these Databases questions