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);
};
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.