Nicholas
Nicholas

Reputation: 3784

Overloading comparison operators for stacks

I need some help here: I'm asked to do some overloading to comparison operators of 2 stacks. I have the syntax figured out, I'm just having trouble writing the definition. So please help me.

At least to one operator overload and then I will do it for the rest.

struct linklist
{
    int no;
    struct linklist *next;
};

class Stack
{
private:
    linklist *list,*head;

public://constructor and destructor
    Stack();
    ~Stack();
public:// main functions
    void push();
    void show();
    void pop();

public://overloaded operations

    friend bool operator == (const Stack &stack1, const Stack &stack2);
    friend bool operator != (const Stack &stack1, const Stack &stack2);
    friend bool operator < (const Stack &stack1, const Stack &stack2);
    friend bool operator > (const Stack &stack1, const Stack &stack2);

};

Upvotes: 0

Views: 1791

Answers (1)

KlaFier
KlaFier

Reputation: 159

It really depends on what you actually want to compare. Is it identity of stacks or just number of elements on stacks? Since you want to define smaller and greater operators, I assume you want to compare the number of elements in the stacks.

The equal operator would be like this:

bool operator==( const Stack &stack1, const Stack &stack2)
{
  return stack1.list->no == stack2.list->no;
}

Of course you need to consider cases where the list member of a Stack object is NULL.

Upvotes: 1

Related Questions