Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Create a method on the Queue class that calculates the number of times a given value occurs in the queue. size_t Queue::count( const T

1. Create a method on the Queue class that calculates the number of times a given value occurs in the queue.

size_t Queue::count( const T & data ) const;

image text in transcribed

2. Create a method on the Queue class that determines whether the queue is in descending order.

bool Queue::isDecreasing( ) const;

image text in transcribed

3. Create a method on the Stack class that determines whether a given value occurs consecutively in the stack.

bool Stack::isConsecutive( const T & data ) const;

image text in transcribed

4. Create a method on the Stack class that reverses the values on the stack.

void Stack::reverse();

image text in transcribed

The starter files code is provided below. Do not alter the class definition or driver code in any way. Programs that crash are subject to a 50% penalty. Please submit the class header files only (Queue.h and Stack.h). PLEASE NOTE: You may not use any Standard Template Library (STL) classes for this; use code provided below only.

Please compile the code from your answer and check for errors before submitting code that is not working.

queue.h:

#pragma once

#include

namespace cs20a

{

template

class queue {

public:

queue();

explicit queue(size_t capacity);

queue(const queue &rhs);

~queue();

queue& operator =(const queue & rhs);

bool operator==(queue & rhs) const;

bool operator!=(queue & rhs) const;

void enqueue(const T& val);

T dequeue();

T peek() const;

void clear();

bool empty() const;

bool full() const;

size_t size() const;

void print();

//*** Implement these methods only. ***

size_t count(const T &data) const;

bool isDecreasing() const;

//*** *** *** *** *** *** *** *** ***

private:

T* elements;

size_t _front, _size, _capacity;

void makeEmpty();

void init(size_t capacity = 2);

void copy(const queue& rhs);

void resize(size_t newCapacity);

};

template

queue::queue() {

init();

}

template

queue::queue(size_t capacity) {

init(capacity);

}

template

queue::~queue() {

makeEmpty();

}

template

queue::queue(const queue &rhs) {

init(rhs._capacity);

copy(rhs);

}

template

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

if (this != &rhs) {

clear();

copy(rhs);

}

return *this;

}

template

bool queue::operator==(queue& rhs) const {

if (_size != rhs._size)

return false;

size_t size = 0;

size_t i = _front;

size_t j = rhs._front;

while (size++

if (elements[i] != rhs.elements[j])

return false;

i = ((i + 1) % _capacity);

j = ((j + 1) % rhs._capacity);

}

return true;

}

template

bool queue::operator!=(queue& rhs) const {

return !(this == &rhs);

}

template

void queue::clear() {

makeEmpty();

init();

}

template

bool queue::empty() const {

return _size == 0;

}

template

bool queue::full() const {

return _size == _capacity;

}

template

size_t queue::size() const {

return _size;

}

template

void queue::makeEmpty() {

delete[] elements;

}

template

void queue::init(size_t capacity) {

_front = _size = 0;

_capacity = capacity;

elements = new T[_capacity];

}

template

void queue::copy(const queue& rhs) {

size_t i = rhs._front;

while (_size

enqueue(rhs.elements[i]);

i = ((i + 1) % rhs._capacity);

}

}

template

void queue::resize(size_t minCapacity) {

if (minCapacity

return;

if (minCapacity >= 0)

{

size_t limit = 1;

while (limit

limit

T *tarray = new T[limit];

for (size_t i = 0; i

tarray[i] = elements[(i + _front) % _capacity];

delete[] elements;

elements = tarray;

_capacity = limit;

}

}

template

void queue::enqueue(const T& val) {

if (full())

resize(_size + 1);

elements[(_front + _size) % _capacity] = val;

_size++;

}

template

T queue::dequeue() {

T item = elements[_front];

_front = (_front + 1) % _capacity;

_size--;

return item;

}

template

T queue::peek() const {

return elements[_front];

}

template

void queue::print() {

cout

cout

if (empty())

{

cout

return;

}

size_t i = _front;

size_t rear = (_front + _size) % _capacity;

std::cout ";

do

{

std::cout

i = (i + 1) % _capacity;

if (i != rear)

cout ";

} while (i != rear);

std::cout

}

//*** Implement count() and isDecreasing() here. ***

}

QueueMenu.cpp:

// Menu.cpp : Defines the entry point for the console application.

//

#include

#include "Queue.h"

using namespace std;

using namespace cs20a;

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

CHOICE menu();

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

int value, total;

queue queue;

CHOICE choice;

do {

choice = menu();

switch (choice) {

case MAKEEMPTY:

queue.clear();

break;

case ISEMPTY:

if (queue.empty())

cout

else

cout

break;

case DEQUEUE:

if (queue.empty())

cout

else

cout

break;

case GETFRONT:

if (queue.empty())

cout

else

cout

break;

case ENQUEUE:

cout

cin >> value;

queue.enqueue(value);

break;

case DECREASING:

if (queue.empty()) {

cout

}

else if (queue.isDecreasing())

cout

else

cout

break;

case COUNT:

cout

cin >> value;

total = queue.count(value);

cout

break;

case PRINT:

queue.print();

cout

break;

case OTHER:

cout

}

} while (choice != QUIT);

return(0);

}

CHOICE menu() {

char choice;

CHOICE result;

cout

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 'C':

case 'c':

result = COUNT;

break;

case 'R':

case 'r':

result = DECREASING;

break;

case 'P':

case 'p':

result = PRINT;

break;

default:

result = OTHER;

break;

}

return(result);

}

stack.h:

#pragma once

#include

#include

namespace cs20a

{

template

class stack

{

public:

stack();

explicit stack(size_t capacity);

stack(const stack &rhs);

~stack();

stack& operator =(const stack & rhs);

bool operator==(stack & a) const;

bool operator!=(stack & a) const;

void push(const T& val);

T pop();

T peek() const;

void clear();

bool empty() const;

bool full() const;

size_t size() const;

void print();

//*** Implement these methods only. ***

bool isConsecutive(const T &data) const;

void reverse();

//*** *** *** *** *** *** *** *** ***

private:

T *elements;

size_t _top;

size_t _capacity;

void makeEmpty();

void init(size_t capacity = 2);

void copy(const stack& rhs);

void resize(size_t newCapacity);

};

template

stack::stack() {

init();

}

template

stack::stack(size_t capacity) {

init(capacity);

}

template

stack::stack(const stack &rhs) {

init(rhs._capacity);

copy(rhs);

}

template

stack::~stack()

{

makeEmpty();

}

template

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

if (this != &rhs) {

clear();

copy(rhs);

}

return *this;

}

template

bool stack::operator==(stack& rhs) const {

if (_top != rhs._top)

return false;

for (size_t i = 0; i

if (elements[i] != rhs.elements[i])

return false;

return true;

}

template

bool stack::operator!=(stack& rhs) const {

return !(this == &rhs);

}

template

bool stack::empty() const {

return (_top == 0);

}

template

bool stack::full() const

{

return (_top == _capacity);

}

template

void stack::clear() {

makeEmpty();

init();

}

template

size_t stack::size() const {

return _top;

}

template

void stack::makeEmpty() {

delete[] elements;

}

template

void stack::init(size_t capacity) {

_top = 0;

_capacity = capacity;

elements = new T[capacity];

}

template

void stack::copy(const stack& rhs) {

for (size_t i = 0; i

push(rhs.elements[i]);

}

template

void stack::resize(size_t minCapacity) {

if (minCapacity

return;

if (minCapacity >= 0)

{

size_t limit = 1;

while (limit

limit

T *tarray = new T[limit];

for (size_t i = 0; i

tarray[i] = elements[i];

delete[] elements;

elements = tarray;

_capacity = limit;

}

}

template

void stack::push(const T& val) {

if (full())

resize(_top + 1);

elements[_top++] = val;

}

template

T stack::pop() {

return elements[--_top];

}

template

T stack::peek() const {

return elements[_top - 1];

}

template

void stack::print()

{

std::cout

if (empty())

{

std::cout

return;

}

std::cout ";

for (size_t i = 0; i

std::cout

if (i

cout ";

}

cout

}

//*** Implement isConsecutive() and reverse() here. ***

}

StackMenu.cpp:

// Menu.cpp : Defines the entry point for the console application.

//

#include

#include "stack.h"

using namespace std;

using namespace cs20a;

enum CHOICE { MAKEEMPTY, PUSH, ISEMPTY, POP, TOP, CONSECUTIVE, REVERSE, QUIT, PRINT, OTHER };

CHOICE menu();

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

int value;

stack stack;

CHOICE choice;

do {

choice = menu();

switch (choice) {

case MAKEEMPTY:

stack.clear();

break;

case ISEMPTY:

if (stack.empty()) {

cout

}

else {

cout

}

break;

case TOP:

if (stack.empty())

cout

else

cout

break;

case POP:

if (stack.empty())

cout

else

cout

break;

case PUSH:

cout

cin >> value;

stack.push(value);

break;

case CONSECUTIVE:

cout

cin >> value;

if (stack.isConsecutive(value)) {

cout

}

else {

cout

}

break;

case REVERSE:

if (!stack.empty())

stack.reverse();

break;

case PRINT:

stack.print();

cout

break;

case OTHER:

cout

}

} while (choice != QUIT);

return(0);

}

CHOICE menu() {

char choice;

CHOICE result;

cout

cin >> choice;

switch (choice) {

case 'M':

case 'm':

result = MAKEEMPTY;

break;

case 'S':

case 's':

result = ISEMPTY;

break;

case 'U':

case 'u':

result = PUSH;

break;

case 'O':

case 'o':

result = POP;

break;

case 'T':

case 't':

result = TOP;

break;

case 'Q':

case 'q':

result = QUIT;

break;

case 'C':

case 'c':

result = CONSECUTIVE;

break;

case 'R':

case 'r':

result = REVERSE;

break;

case 'P':

case 'p':

result = PRINT;

break;

default:

result = OTHER;

break;

}

return(result);

}

Example: count function Result Queue int> count (5) 3 front->(1) (1) (5) (3) (5) (5) (2) K-back count (10) 0 front- (1) (1) (5) (3) (5) (5) (5) (2) -back

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

Data Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

More Books

Students also viewed these Databases questions