0

I need to reverse all nodes in my list.

My code:

#include<iostream>
#include<stdlib.h>
using namespace std;
struct elem
{
    int inf;
    elem *adr;
    elem(int n, struct elem *p): inf(n),adr(p) {}
};
class list
{
    elem *prim,*p;
public:
    void afisare_lista ();
    list(void)
    {
        prim=NULL;
    };
    list(int);
    list operator-() const;
    list operator-(elem *current);
};

list::list(int nr)
{
    int inf;
    cin>>inf;
    prim=new elem(inf,NULL);
    elem*q=prim;
    for(int i=1; i<nr; i++)
    {
        cin>>inf;
        p=new elem(inf,NULL);
        q->adr=p;
        q=p;
    }
    p=prim;
}
void list::afisare_lista()
{
    elem *t;
    cout<<"\n";
    t=prim;
    while(t!=NULL)
    {
        cout<<t->inf<<" ";
        t=t->adr;
    }
}
list list::operator-() const
{
    elem *v;
    v = prim;
    while (v!=NULL)
    {
        cout<<-v->inf<<" ";
        v = v->adr;
    }

}

list list::operator-(elem *current)
{
    current = prim;
    elem* prev = NULL, *next = NULL;

    while (current != NULL)
    {
        next = current->adr;
        current->adr = prev;
        prev = current;
        current = next;
    }
    prim = prev;

}

int main()
{
    list l(6);
    l.afisare_lista();
    -l;
    cout<<"\n";
}

For example:

Input: 1 2 3 4 5 6

Output: 6 5 4 3 2 1

not sure what it is "minimal reproducible example", but is full code. Second function(list list::operator-(elem *current)) I want to use for revers list. First for multiply every node with -1 (list list::operator-() const)

How can i call the second method for reverse my list.

  • It looks like a singly linked list, correct? Do you have a head and tail pointer? What member functions do you have to add elements to the list? Make a [mcve] please. – Ted Lyngmo Nov 30 '20 at 12:05
  • Why have you overloaded the binary minus (that is, subtraction) operator? You're ignoring the parameter, and it is very surprising for a subtraction to do that and modify its left operand (and promise to return something and then not do that). – molbdnilo Nov 30 '20 at 12:07
  • @molbdnilo I think OP wants to use unary minus to return a new, but reversed, list. – Ted Lyngmo Nov 30 '20 at 12:09
  • @TedLyngmo, I don't even know how to explain. Okay, i will try. For example i have 2 tasks. 1)Use overload of operator '-' to reverse list. 2) Use overload of operator '-' to multiply every node of list with -1. In second task i use operator- without parameters. But first task. How call function if exist one with the same name – Женя Касьян Nov 30 '20 at 12:19
  • The answer to the last question is that the functions have different signatures so the compiler will match your call to the function with the correct signature. `list newlist = -oldlist;` matches `list list::operator-() const` and `list newlist = oldlist - elemptr;` would match the first (although I find that signature a bit odd). Put a [mcve] in the question and it'll be easier to answer. – Ted Lyngmo Nov 30 '20 at 12:27
  • 2
    You should [edit] your original question to include the code, rather than adding an answer. (Answers are for, well, *answers* to the question.) – Sneftel Nov 30 '20 at 13:01
  • @Sneftel, sorry, modified – Женя Касьян Nov 30 '20 at 13:06
  • here are quite a few of the basic member functions missing that I think you should implement before implementing the reversal method. I made a little [example](https://godbolt.org/z/fzoT5T) that you can play around with and perhaps get some ideas from. – Ted Lyngmo Nov 30 '20 at 13:58
  • 1
    you're `using namespace std;` and then declare `class list` which has the same name as `std::list` which is not a good idea. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/995714) – phuclv Nov 30 '20 at 16:35

1 Answers1

1

Your overlaod operator is list list::operator-(elem *current) . it is an arithmetic form of list a - elem * b as follows:

int main()
{
   list l(6);
   std::cout << "Origin sequence : "; l.afisare_lista(); std::cout<<std::endl;
   elem* a = new elem(0, NULL);
   l - a;
   std::cout << "Reverse sequence: "; l.afisare_lista(); std::cout<<std::endl;
}

It renders the result:

1 2 3 4 5 6
Origin sequence :
1 2 3 4 5 6
Reverse sequence:
6 5 4 3 2 1
Although the return of overlaod operators are irrelavant to your purpose, it looks good to get rid of the compiler warning by returning itself following the convention:
//
  class list
  {
    elem *prim,*p;
    public:
    void afisare_lista ();
    list(void);
    list(int);
    list& operator-();
    list& operator-(elem *current);
     //add "return *this;" at the end of these functions
     // It make no difference but stop the warning messages.
   };
ytlu
  • 412
  • 4
  • 9