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.