Question
Working With Stacks (COMPLETE EITHER 2B OR 2C) Complete the following exercises using the cs20::Stack class presented in class. 1. Create a new method on
Working With Stacks (COMPLETE EITHER 2B OR 2C)
Complete the following exercises using the cs20::Stack class presented in class.
1. Create a new method on a Stack that determines if all the values on the Stack are smaller than the passed value. For example, for a stack containing top- (1) (1) (2) (3) (5) (5) (2) , allSmaller( 3 ) should return false because there are values on the Stack bigger than 3. On the other hand, for a stack containing top- (10) (11) (20) (30) (50) (15) (12) , allSmaller( 100 ) should return true because all the values on the stack are smaller than 100. If the stack is empty, this new operation should return true.
This new operation of the Stack class should have the signature:
bool allSmaller( const Object & data ) const;
Because it has been marked const, this method is saying that it is read-only. When your implementation runs, you won't be able to actually change any data members of the Queue passed to your method. Create a test driver program that exercises each of these exercises to satisfy yourself that your implementation is correct. For example, you should print the Queue before and after invoking the operation.
TESTING HINT: Run the methods: push( 3 ); push( 2 ); push( 1 ); Print the queue. What should it look like? Run the method: allSmaller( 1 ). It should return false. Does it? Run the method: allSmaller( 0 ). It should return false. Does it? Run the methods: push( 4 ); push( 1); push( 100 ); Print the queue. What should it look like? Run the method: allSmaller( 101 ). It should return true. Does it? Run the methods: pop( ); allSmaller( 5 ). It should return true. Does it? Run the methods: pop(); pop(); Print the queue. What should it look like?
2. Create a new method on a Stack that determines if how many times a passed value is found on the stack. For example, for a stack containing top- (1) (1) (2) (3) (5) (5) (2) , count( 5 ) should return 2 because it is there just once on the stack. On the other hand, for a stack containing top- (1) (1) (2) (3) (5) (5) (2) , count( 12 ) should return 0 because there are no values on the stack that equal 12.
This new operation of the Stack class should have the signature:
int count( const Object & data ) const;
Because it has been marked const, this method is saying that it is read-only. When your implementation runs, you won't be able to actually change any data members of the Stack passed to your method. Create a test driver program that exercises each of these exercises to satisfy yourself that your implementation is correct. For example, you should print the Stack before and after invoking the operation.
TESTING HINT: Run the methods: push( 3 ); push( 2 ); push( 1 ); Print the queue. What should it look like? Run the method: count( 1 ). It should return 1. Does it? Run the method: count( 0 ). It should return 0. Does it? Run the methods: push( 4 ); push( 100 ); push( 1 ); Print the queue. What should it look like? Run the method: count( 1 ). It should return 2. Does it? Run the method: count( 100 ). It should return 1. Does it? Run the methods: pop(); pop(); Print the queue. What should it look like?
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
here's the code:
StackMenu.cpp
// Menu.cpp : Defines the entry point for the console application.
//
#include
#include "Stack.h"
#include "EmptyStack.h"
#include "StackNode.h"
#include "Stack.cpp"
#include "StackNode.cpp"
using namespace std;
using namespace cs20;
enum CHOICE {MAKEEMPTY, PUSH, ISEMPTY, POP, TOP, TOPANDPOP, QUIT, PRINT };
CHOICE menu();
int main(int argc, char* argv[]) {
int value;
Stack
CHOICE choice;
do {
choice = menu();
switch( choice ) {
case MAKEEMPTY:
stack.makeEmpty();
break;
case ISEMPTY:
if (stack.isEmpty()) {
cout << "stack is empty" << endl;
}
else {
cout << "stack is not empty" << endl;
}
break;
case TOP:
try {
value = stack.top();
cout << "Here's the value on top: ";
cout << value << endl;
} catch( EmptyStack es ) {
cout << "You silly... don't try topping an empty stack!" << endl;
}
break;
case POP:
try {
stack.pop();
} catch( EmptyStack es ) {
cout << "You silly... don't try popping an empty stack!" << endl;
}
break;
case PUSH:
cout << "Please provide int to push: ";
cin >> value;
stack.push( value );
break;
case TOPANDPOP:
try {
value = stack.topAndPop();
cout << "Here's the value on top that got popped: ";
cout << value << endl;
} catch( EmptyStack es ) {
cout << "You silly... don't try topAndPopping an empty stack!" << endl;
}
break;
case PRINT:
stack.printStack( cout );
cout << endl;
break;
case QUIT:
break;
}
} while (choice != QUIT);
return( 0 );
}
CHOICE menu() {
char choice;
CHOICE result;
cout << "(M)akeEmpty I(s)Empty p(U)sh p(O)p (T)op top(A)ndpop (P)rint (Q)uit: " << endl;
cin >> choice;
switch( choice ) {
case 'A':
case 'a':
result = TOPANDPOP;
break;
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 'P':
case 'p':
result = PRINT;
break;
}
return( result );
}
void sample() {
Stack
Stack
cout << "making s1" << endl;
s1.push( 1 );
s1.push( 2 );
cout << "print s1" << endl;
s1.printStack( cout );
cout << endl;
cout << "s2 = s1" << endl;
s2 = s1;
cout << "print s2" << endl;
s2.printStack( cout );
cout << endl;
s1.pop();
cout << "pop s1" << endl;
cout << "print s2" << endl;
s2.printStack( cout );
cout << endl;
cout << "print s1" << endl;
s1.printStack( cout );
cout << endl;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
StackNode.cpp
#ifndef STACKNODE_CPP
#define STACKNODE_CPP
#include "StackNode.h"
namespace cs20 {
template
StackNode
StackNode
element = theElement;
next = node;
}
template
const Object& StackNode
return( element );
}
template
StackNode
return( next );
}
template
void StackNode
next = node;
}
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Stack.cpp
#ifndef STACK_CPP
#define STACK_CPP
#include "Stack.h"
namespace cs20 {
template
Stack
topNode = nullptr;
}
template
Stack
topNode = nullptr;
*this = rhs;
}
template
Stack
makeEmpty();
delete topNode;
}
template
bool Stack
return( (topNode == nullptr) );
}
template
void Stack
while (!isEmpty()) {
pop();
}
}
template
void Stack
StackNode
topNode = newNode;
}
template
void Stack
if (isEmpty()) {
throw EmptyStack();
}
StackNode
topNode = topNode->getNext();
delete oldTop;
}
template
const Object& Stack
if (isEmpty()) {
throw EmptyStack();
}
StackNode
return( node.getElement() );
}
template
Object Stack
Object o = top();
pop();
return( o );
}
// Deep copy of linked Stack
template
const Stack
if (this != &rhs) {
makeEmpty();
if (!(rhs.isEmpty())) {
StackNode
StackNode
topNode = myTopNode;
rhsTopNode = rhsTopNode->getNext();
while (rhsTopNode != nullptr) {
myTopNode->setNext( new StackNode
myTopNode = myTopNode->getNext();
rhsTopNode = rhsTopNode->getNext();
}
}
}
return( *this );
}
template
std::ostream& Stack
if (isEmpty()) {
outs << "Empty Stack";
}
else {
outs << "TOP: ";
StackNode
while (node != nullptr) {
outs << node->getElement();
outs << " "; /// for visual alignment
node = node->getNext();
}
}
return( outs );
}
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Stack.h
#ifndef STACK_H
#define STACK_H
#include
#include "StackNode.h"
#include "EmptyStack.h"
namespace cs20 {
template
class Stack {
public:
Stack();
Stack( const Stack& rhs );
~Stack();
bool isEmpty() const;
void makeEmpty();
void push( const Object& data );
void pop();
const Object& top() const;
Object topAndPop();
std::ostream& printStack( std::ostream& outs ) const;
const Stack& operator =( const Stack& rhs );
private:
StackNode
};
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
StackNode.h
#ifndef STACKNODE_H
#define STACKNODE_H
#include
namespace cs20 {
template
class StackNode {
public:
StackNode( const Object& theElement = Object(), StackNode * node = nullptr );
const Object& getElement() const;
StackNode* getNext() const;
void setNext( StackNode* node );
private:
Object element;
StackNode* next;
};
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
EmptyStack.cpp
#include "EmptyStack.h"
namespace cs20 {
EmptyStack::EmptyStack( const std::string& msg ) : std::logic_error( msg.c_str() ) {}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
EmptyStack.h
#ifndef EMPTYSTACK_H
#define EMPTYSTACK_H
#include
#include
namespace cs20 {
class EmptyStack : public std::logic_error {
public:
EmptyStack( const std::string& msg = "" );
};
}
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started