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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!