Question
CSC 240 Lab 5 StackType Implement a function that replaces each occurrence of a particular item (oldItem) in a stack with another item (newItem). Use
CSC 240
Lab 5
StackType
Implement a function that replaces each occurrence of a particular item (oldItem) in a stack with another item (newItem). Use the following specification:
ReplaceItem(StackType& stack, ItemType oldItem, ItemType newItem)
Function: Replaces all occurrences of oldItem with newItem.
Precondition: stack has been initialized.
Postcondition: Each occurrence of oldItem in stack has been replaced by newItem.
Implement the following specification for a client boolean function that returns true if two stacks are identical and false otherwise.
bool Identical(const StackType& stack1, const StackType& stack2)
Function: Determines if two stacks are identical.
Preconditions: stack1 and stack2 have been initialized.
Postconditions: stack1 and stack2 are unchanged.
Function return value = stack1 and stack2 are identical?
Implement these functions using the non-template dynamic array-based version of StackType.
These functions are declared and implemented in the client program.
Use the following driver to test your functions:
#include
#include
#include "StackType.h"
using namespace std;
void ReplaceItem(StackType& stack, ItemType oldItem, ItemType newItem){
//Function: Replaces all occurrences of oldItem with newItem. /
/Precondition: stack has been initialized.
//Postcondition:Each occurrence of oldItem in stack has been replaced by newItem.
}
bool Identical(const StackType& stack1, const StackType& stack2){
//Function: Determines if two stacks are identical.
//Preconditions: stack1 and stack2 have been initialized.
//Postconditions: stack1 and stack2 are unchanged.
// Function return value = stack1 and stack2 are identical
return true; //they are the same
}
int main()
{
try{
StackType stack;
stack.Print();
stack.Push(4);
stack.Push(4);
stack.Push(5);
stack.Push(4);
stack.Push(3);
stack.Push(4);
stack.Print();
ReplaceItem(stack, 4, 8);
stack.Print();
StackType myStack;
myStack.Print();
myStack.Push(4);
myStack.Push(4);
myStack.Push(5);
myStack.Push(4);
myStack.Push(3);
myStack.Push(4);
myStack.Print();
if(Identical(stack, myStack)){
cout << "The stacks are identical." << endl;
}
else{
cout << "The stacks are NOT identical." << endl;
}
}
catch ( const FullStack& e )
{
cout << "Stack is full!" << endl;
}
catch(const EmptyStack& e)
{
cout << "Stack is empty!" << endl;
}
return 0;
}
Declare the following functions in the class header file:
void ReplaceItem(StackType& stack, ItemType oldItem, ItemType newItem);
friend bool Identical(const StackType& stack1, const StackType& stack2);
Note, the ReplaceItem function is not a friend function, so it will not have direct access to the StackType data members.
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