0

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);

};
dyp
  • 38,334
  • 13
  • 112
  • 177
Nicholas
  • 3,529
  • 2
  • 23
  • 31
  • 2
    Everything that you want to know about operator overloading is [here](http://stackoverflow.com/questions/4421706/operator-overloading) – digital_revenant Oct 24 '13 at 20:09
  • " im just having trouble writing the definition" What have you tried? What went wrong? Can you show us an error message? – dyp Oct 24 '13 at 20:10
  • Yes i tryed like this , return stack1 == stack2 – Nicholas Oct 24 '13 at 20:16
  • 1
    @Nicholas That creates an endless loop (or endless recursion), right? You need to implement it by comparing the members of the class in the correct way. Once you managed to implement `==` and when you want to implement `!=`, remember that `a!=b` should be the same as `!(a==b)`. Good luck! – Daniel Frey Oct 24 '13 at 20:19
  • @Nicholas Well that is what *you* actually shall implement. First, define what `==` shall mean. For example: shall it mean identity (`stack1 == stack2` IFF `&stack1 == &stack2`), shall it mean that the elements are equal, .. Then, implement the comparison function accordingly. – dyp Oct 24 '13 at 20:21
  • im confused ;( , guys i know what != and == , but the problem is i cand figure it out what to write in the body . – Nicholas Oct 24 '13 at 20:25
  • 1
    Well an overloaded operator can return anything. You could write just `friend bool operator == (const Stack &stack1, const Stack &stack2) { return true; }` but that wouldn't be very useful. It would be useful for example if the operator returns true if and only if the two stacks have the same number of elements, and element number #i of stack 1 is equal to element #i of stack 2 (for all i from 0 to the total number of elements). – dyp Oct 24 '13 at 20:31
  • Thx for hints , i will try harder. – Nicholas Oct 24 '13 at 20:38

1 Answers1

1

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.

KlaFier
  • 159
  • 2
  • Yes , you are right , i want to compare the number of elements . – Nicholas Oct 24 '13 at 20:50
  • There are several possible comparisons, you could compare that the stacks are the same object, the number of elements are the same, the stack elements match to size of the smaller stack, or the stacks match exactly. – ChuckCottrill Oct 24 '13 at 20:53