Implement the following specification for a client Boolean f


Implement the following specification for a client Boolean function that returns true if two stacks identical and false otherwise. You can use all stack member functions as discussed in class. Boolean Identical(stacktype s1, stacktype s2);

Solution

Please find the required program. Please see the comments against each line to understand the step.

bool Identical(stacktype<int> s1, stacktype<int> s2) {
   int lh, rh;
   int ret = 0;
   if ( (lh = s1.pop()) == (rh = s2.pop()) ) {       //check if both stacks top element is same or not
       if ( !s1.isEmptyStack() and !s2.isEmptyStack() ) //if both tops are same, then pop the top element and recursively call Identical method on the rest elements of the stacks
           return Identical(s1, s2);
       else
           ret = 1;   //if both are not empty that means both are not same
   }
   s1.push(lh);   //add the poped element back to stack to keep the stacks element back into it
   s2.push(rh);
   return ret;
}

 Implement the following specification for a client Boolean function that returns true if two stacks identical and false otherwise. You can use all stack member

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site