There may be many ways to do it, share the most simple way.
The first is the node and link list structure:
struct Mynode {int value; Mynode * NEXT; };
struct MyList {Mynode * first; Mynode * LAST; };
Provides some basic functions:
void List_init (MyList * container) {Container->first = 0; Container->last = 0; }
BOOL List_isempty (MyList * container) {return 0 = = container->first;}
Mynode * List_first (MyList * container) {return container->first;}
Mynode * List_next (Mynode * node) {return node->next;}
void List_pushback (MyList * container, Mynode * node) {if (List_isempty (container)) {Container->first = node; container->last = node; } else {container->last->next = node; container->last = node; } node->next = 0; }
Mynode * List_popfront (MyList * container) {Mynode * FrontPoint = container->first; Container->first = frontpoint->next; return frontpoint; }
The key is to reverse the link List:
void List_reverse (MyList * container)
{Mynode * Tempfirst = container->first;
Mynode * Templast = container->last;
if (tempfirst! = templast) {container->last = Tempfirst;
Mynode * Frontnode = container->first; Mynode * NextNode = frontnode->next; while (NextNode! = 0) {Mynode * temp = nextnode->next; Nextnode->next = Frontnode; Frontnode = NextNode; NextNode = temp; }
Container->first = Templast; Container->last->next = 0; } }
The main function validation works correctly:
int _tmain (int argc, _tchar* argv[]) {mylist myinitiallist; List_init (&myinitiallist);
for (int t = 1; t <=; t++) {Mynode * temp = (Mynode *) malloc (sizeof (Mynode)); Temp->value = t; List_pushback (&myinitiallist, temp); }
Mynode * a = List_first (&myinitiallist); while (a) {printf ("%d \ n", a->value); A = List_next (a); }
List_reverse (&myinitiallist); Mynode * b = List_first (&myinitiallist); while (b) {Mynode * x = b; printf ("%d \ n", b->value); b = List_next (b); Free (x); }
return 0; }
Implementing reverse link List with C + + code