Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The goal of this assignment is to complete question 15 in your textbook on stack

ID: 3804391 • Letter: T

Question

The goal of this assignment is to complete question 15 in your textbook on stacks. You must use the authors node2.h, node2.template, stack4.h and stack4.template

You can find the node files here: https://www.cs.colorado.edu/~main/chapter6/

You can find the stack files here: https://www.cs.colorado.edu/~main/chapter7/

Problem 15 states: For any of your stack implementations, please write a new member function called swap with one reference parameter that is also a stack. After calling x.swap(y), the value of x should equal the original value of y and vice versa. Make sure to print out the values before the swap and after the swap as well.

Explanation / Answer

void swap(stack& val_y)
{
int val_p=1;
if(!Empty())
{
for (int val_i = 0; val_i<=val_y.top; val_i++)
{
int val_q=val_y.elements[val_i];
for(int val_k=0;val_k<=top;val_k++)
{
if(elements[val_k]==val_q)
{
val_p=0;
break;
}
}
if(val_p==1)
{
push(val_q);
}
}
  
for (int val_i = 0; val_i<=top; val_i++)
{
int val_q=elements[val_i];
for(int val_k=0;val_k<=val_y.top;val_k++)
{

if(val_y.elements[val_k]==val_q)
{
val_p=0;
break;
}
}
if(val_p==1)
{
val_y.push(val_q);
}
}
}
else
{
(this)->top = val_y.top;

for (int val_i = 0; val_i<=val_y.top; val_i++)
{
int val_q=val_y.elements[val_i];
push(val_q);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote