stacks and how to check if they are equal

I want to check if a stack is equal using stack.top etc to run down two stacks in a while loop anyone have a good example . I was thinking like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  template <class Type>
bool isEqual(linkedStackType<Type>& stack1, linkedStackType<Type>& stack2)
{
    
  while (stack1.isEmptyStack()==false)
   {
  stack1.top();
      stack1.push();
     // stack1.pop();
      //cout <<  << " ";
   }

   cout << endl;

  // stack1 = tmpStack;    
Would you mind posting your custom class linkedStackType definition?
What methods/functions are available to be called?
Yes kevin is right.
I would imagine this will be implemented in terms of a recursive function. Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
typedef linkedStackType stack;
template <typename Type>
bool isEqual(stack<Type> &stack1, stack<Type> &stack2) {
	Type lh, rh;
	int ret = 0;
	if ( (lh = stack1.pop()) == (rh = stack2.pop()) ) {
		if ( !stack1.isEmptyStack() and !stack2.isEmptyStack() ) return isEqual(stack1, stack2);
		else ret = 1;
	}
	stack1.push(lh);
	stack2.push(rh);
	return ret;
}
Last edited on
Topic archived. No new replies allowed.