b3.pdf CSC 326- Lab 3: Implementing a Stack Using a Run-Time Array 1. Given the
ID: 3593844 • Letter: B
Question
b3.pdf 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. 2. Implement the following operation as a member function of the Stack Type class replace(ItemType oldltem, ItemType newltem) Function Precondition Replaces all occurrences of oldltem with newltem. Stack has been initialized. Postconditions Each occurrence of oldltem in stack has been changed to newltem Write the function replace(ltemType oldltem, ItemType newltem) as a new member function of the StackType. Write the function replace(ltem Type oldltem, ItemType newltem) as a nonmember function of the StackType 3. Implement the following operation as a member function of the StackType class bool identical(StackType& stackI) Function Determines if stackl and self are identical. stackI and self have been initialized Precondition Postconditions: stackI and self are unchanged. Function value-(self and stack are identical). Write the function bool identical(Stack Typed stackl) ) as a new member function of 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
#include<iostream>
using namespace std;
class StackType{
int top;
int capacity;
int *stack_array;
public:
StackType()
{
cout<<"Enter the Capacity of Stack";
cin>>capacity;
stack_array=new int[capacity];
top=-1;
}
bool Is_empty()
{
if(top==-1)
return 1;
else return 0;
}
bool Is_full()
{
if(top==capacity-1)
return 1;
else return 0;
}
void push(int val)
{
if(Is_full()){
cout<<"No insertion is possible ";
return;}
top++;
stack_array[top]=val;
}
int pop()
{
if(Is_empty())
{
cout<<"No deletion is possible ";
return -99999;
}
return(stack_array[top--]);
}
void replace(int olditem, int newitem)
{
int i;
if(!Is_empty())
for(i=0;i<=top;i++){
if(olditem==stack_array[i])
stack_array[i]=newitem;}
else
cout<<"Stack is empty or not initialised";
}
bool identical(StackType&stack1)
{
bool b=1;
int i;
if((!Is_empty()&&!stack1.Is_empty())&&(top==stack1.top))
for(i=0;i<=top;i++)
{
if(stack1.stack_array[i]!=stack_array[i])
{
b=0;
break;
}
}
else
{
b=0;
}
return b;
}
void display()
{
int i;
if(!Is_empty())
for(i=top;i>=0;i--)
cout<<stack_array[i]<<" ";
else
cout<<"Stack is not initialised or empty";
cout<<endl;
}
friend int top(StackType);
friend void replace(StackType, int, int);
};
void replace(StackType s,int olditem, int newitem)
{
int i;
if(!s.Is_empty())
for(i=0;i<=s.top;i++){
if(olditem==s.stack_array[i])
s.stack_array[i]=newitem;
}
else
cout<<"Stack is not initialised or empty ";
}
int top(StackType s)
{
if(!s.Is_empty())
return s.stack_array[s.top];
else{
cout<<"Stack is not initialised or empty ";
return -99999;
}
}
int main()
{
StackType s1,s2;
bool b;
s1.push(10);
s1.push(20);
s1.push(30);
s2.push(10);
s2.push(20);
s2.push(30);
//s2.push(300);
b=s1.identical(s2);
if(b)
cout<<" Stacks are identical ";
else
cout<<"Stacks are not identical ";
s1.display();
s1.replace(10,50);
s1.display();
cout<<top(s1)<<endl;
replace(s1,20,100);
s1.display();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.