[35 marks] a) The pointer based StackType class is to be extended with a method
ID: 3600223 • Letter: #
Question
[35 marks] a) The pointer based StackType class is to be extended with a method called Replaceltem that replaces each copy of an item by another item. A reference to the stack object as well as copies of both items are passed as parameters to the method. You can use any of the methods of the StackDR Class in your implementation: IsEmptu0. Pop0, Top(ItemType) and Push(ItemType item). [30 marks] Void StackTyReReplaceltem(Stack& stack, ItemType item1, ItemType item2) b) Write a line of code that checks whether an array based StackType called stack is Empty (you are not allowed to use the member functiorn [l mark] c) Write a line of code that checks whether an array based QueuType called queue is full (you are not [1 mark] allowed to use the member function IsFullo d) Write 1 line of code that checks whether an array base-QueusTyp, called queue is Empty (You are 1 mark not allowed to use the member function e) Write 1 line of code that checks whether a pointer based QueueType called queue is full (you are not allowed to use the member function tsEulo [l markj f) Write 1 line of code that checks whether a pointer based QueueType called queue is Empty (You are not allowed to use the member function mark Activate ECCE342-Midterm 25-Oct-201 to6stExplanation / Answer
a) Code for ReplaceItem() method is as follows:
void StackType::ReplaceItem(Stack& stack, ItemType item1, ItemType item2)
{
// max size of array A
const int MAX = 1000000;
int i = -1;
ItemType A[MAX];
// looping till stack is not empty
while(!stack.IsEmpty())
{
ItemType x;
// popping the stack top and storing it to x
x = stack.pop();
// replacing item1 by item2
if(x == item1)
x = item2;
// incrementing i and storing x to A[i]
i++;
A[i] = x;
}
// looping untill all the elements of array A have been pushed back to the stack
while(i>=0)
{
stack.push(A[i]);
i--;
}
}
b) Code for Empty() member function is as follows:
bool StackType::Empty()
{
// head is the variable,
// it points to the top of the stack
// when an element is popped, value of head is decremented by 1
// when stack becomes empty, head is equal to -1
if( head == -1 )
return true;
else
return false;
}
c) Code for Full() member function for QueueType queue is as follows:
bool QueueType::Full()
{
// rear is the variable,
// it points to the side of the queue from where elemeted are pushed in
// when an element is pushed, value of rear is incremented by 1
// when queue becomes full, rear is equal to MAX-1
// MAX is the maximum size of the queue
if( rear == MAX-1 )
return true;
else
return false;
}
d) Code for Empty() member function for QueueType queue is as follows:
bool QueueType::Full()
{
// rear is the variable,
// it points to the side of the queue from where elemeted are pushed in
// when an element is pushed, value of rear is incremented by 1
// when queue becomes full, rear is equal to MAX-1
// MAX is the maximum size of the queue
if( rear == MAX-1 )
return true;
else
return false;
}
Accordind to chegg policy, only top four questions can be answered at a time.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.