The goal of this assignment is to reinforce stacks in C++. Write a function that
ID: 3675757 • Letter: T
Question
The goal of this assignment is to reinforce stacks in C++.
Write a function that compares two stacks for equality. The function takes two stacks as parameters and returns true if they are identical. The stacks should remain unchanged after the function returns to the calling program. Hint: Either write a friend function that can examine the elements directly, or pop the stacks and save the popped elements so that the stacks can be restored. In this second case, the stack parameters will not be const reference parameters because they are temporarily changed before being restored.
As an alternative, the parameters could be value parameters (so that changing them does not affect the actual arguments).
Which of the three approaches (friend function, reference parameters, or value parameters) will be most efficient? You need to have a test program to test your code.
Explanation / Answer
#include<iostream>
#include<stdlib.h>
#include"stack.h"
usingnamespace std;
int main()
{
stackType<int> s1;
stackType<int> s2;
if(s1==s2)
{
cout<<"empty stacks are equal"<<endl;
}
s1.push(10);
s1.push(20);
s2.push(10);
cout<<"s1:";
cout<<"s2:";
cout<<"s1:";
if (s1!=s2)
cout<<"!";
cout<<"=s2"<<endl;
s2.push(20);
cout<<"s2:";
cout<<"s1:";
if(s1==s2)
{
cout<<"is equal";
}
else
{
cout<<"is not equal"<<endl;
}
cout<<"s2"<<endl;
s1,pop();
s1.pop();
s1.push(30);
s1.push(20);
cout<<"is";
cout<<"not equal to s2"<<endl;
system("pause");
return 0;
}
value parameter is most efficient
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.