CSC 326-Lab 3: Implementing a Stack Using a Run-Time Array 1. Given the followin
ID: 3593845 • Letter: C
Question
CSC 326-Lab 3: Implementing a Stack Using a Run-Time Array 1. Given the following specification of the top operation: ItemType top Function Precondition Postconditions: Function value - copy of item at top of stack. Stack is not changed Returns copy of the last item put onto the stack. Stack is not empty Write the function top as a nonmember function of the StackType. Implement the following operation as a member function of the StackType class replace(ItemType oldltem, ItemType newltem) 2. Replaces all occurrences of oldltem with newltem. Stack has been initialized. Function: Precondition Postconditions: Each occurrence of oldltem in stack has been changed to newltem. Write the function replace( Item Type oldItem. ItemType newItem) as a new member function of the StackType. write the function replace(ItemType oldItem. ItemType newItem) as a nonmember function of the StackType 3. Implement the following operation as a member function of the StackType class bool identical(StackType& stackl) Function: Determines if stackI and self are identical. stackI and self have been initialized. Precondition: Postconditions stackl and self are unchanged. Function value (self and stackl are identical). function of Write the function bool identical(StackTyped stackI)) as a new member the StackType. Write a member function display to output the values stored in the stack Write a test driver (epp) file to test all member and nonmember functions.Explanation / Answer
Given below is the code with output. Please do rate the answer if it helped. Thank you.
StackType.h
#ifndef StackType_h
#define StackType_h
typedef int ItemType;
class StackType
{
private:
ItemType* data;
int count;
static const int DEFAULT_SIZE = 20;
public:
StackType();
bool push(ItemType item);
void pop();
bool empty();
friend ItemType top(StackType &s); //non-member function
void replace(ItemType oldItem, ItemType newItem); //member function
bool identical(StackType& stack1);
void display();
~StackType();
};
#endif /* StackType_h*/
StackType.cpp
#include "StackType.h"
#include <iostream>
using namespace std;
StackType::StackType()
{
data = new ItemType[DEFAULT_SIZE];
count = 0;
}
bool StackType::push(ItemType item)
{
if(count != DEFAULT_SIZE)
{
data[count++] = item;
return true;
}
else
return false;
}
void StackType::pop()
{
if(!empty())
count--;
}
bool StackType::empty()
{
return count == 0;
}
ItemType top(StackType &s) //non-member function
{
return s.data[s.count-1];
}
void StackType::replace(ItemType oldItem, ItemType newItem) //member function
{
for(int i = 0; i < count; i ++)
{
if(data[i] == oldItem)
data[i] = newItem;
}
}
bool StackType::identical(StackType& stack1)
{
if(count == stack1.count) //check size
{
for(int i = 0; i < count; i++)
{
if(data[i] != stack1.data[i]) //non matching elements
return false;
}
return true;
}
else
return false;
}
void StackType::display()
{
for(int i = count - 1; i >= 0; i--)
cout << data[i] << " " ;
cout << endl;
}
StackType::~StackType()
{
delete []data;
}
test.cpp
#include "StackType.h"
#include <iostream>
using namespace std;
int main()
{
StackType s1;
cout << "pushing 2 4 6 on s1" << endl;
s1.push(2);
s1.push(4);
s1.push(6);
cout << "stack s1 contains " ;
s1.display();
cout << "top(s1) is " << top(s1) << endl;
if(s1.empty())
cout << "stack s1 is empty" << endl;
else
cout << "stack s1 is not empty" << endl;
StackType s2;
cout << "pushing 2 4 6 on s2 " << endl;
s2.push(2);
s2.push(4);
s2.push(6);
cout << "stack s2 contains " ;
s2.display();
if(s1.identical(s2))
cout << "s1 and s2 are identical" << endl;
else
cout << "s1 and s2 are NOT identical" << endl;
cout << "pushing 2 5 6 on s2 " << endl;
s2.push(2);
s2.push(5);
s2.push(6);
cout << "stack s2 contains " ;
s2.display();
if(s1.identical(s2))
cout << "s1 and s2 are identical" << endl;
else
cout << "s1 and s2 are NOT identical" << endl;
cout << "replacing 2 by 8 on s2" << endl;
s2.replace(2, 8);
cout << "s2 contains " ;
s2.display();
cout << "popping all elements of s1" << endl;
while(!s1.empty())
{
cout << "popping " << top(s1) << endl;
s1.pop();
}
}
output
pushing 2 4 6 on s1
stack s1 contains 6 4 2
top(s1) is 6
stack s1 is not empty
pushing 2 4 6 on s2
stack s2 contains 6 4 2
s1 and s2 are identical
pushing 2 5 6 on s2
stack s2 contains 6 5 2 6 4 2
s1 and s2 are NOT identical
replacing 2 by 8 on s2
s2 contains 6 5 8 6 4 8
popping all elements of s1
popping 6
popping 4
popping 2
Program ended with exit code: 0