1. Adopt C Language method
1#include <stdio.h>2#include <stdlib.h>3 4typedefstructnode5 {6 Chardata;7 structNode *Next;8}lnode;//single-linked table node types9 TenLnode *createlinklist ()//generate a single linked list One { ALnode *head,*p,*Q; - Charx; -Head= (lnode*)malloc(sizeof(Lnode));//Generating head nodes thehead->next=NULL; -p=head; -Q=p;//Q always points to the end of the chain -printf"Input any char string:\n"); +scanf"%c",&x); - while(x!='\ n')//generate other nodes of the linked list + { Ap= (lnode*)malloc(sizeof(Lnode)); atP->data=x; -p->next=NULL; -q->next=p; -Q=p;//Q points to the new end of the chain -scanf"%c",&x); - } in returnHead//returns the head pointer pointing to a single linked list - } to + voidConvert (Lnode *h)//single linked list reverse - { theLnode *p,*Q; *p=h->next;//p always points to the first data node of the remaining node list $h->next=null;//new Linked list H is initially emptyPanax Notoginseng while(p!=NULL) - { theQ=p;//Remove the first node from the remaining list of nodes . +p=p->next;//P continues to point to the remaining list of new first data nodes Aq->next=h->next;//inserts the extracted node *q into the new linked list H's chain head theh->next=Q; + } - } $ $ voidPrint (Lnode *h)//Print linked list - { -Lnode *p=h->Next; the while(p!=NULL) - {Wuyiprintf"%c",p->data); theP=p->Next; - } Wuprintf"\ n"); - } About $ voidMain () - { -Lnode *A; -A=createlinklist ();//generate single-linked list a Aprintf"Print input link list: \ n"); + print (A); theprintf"Print reverse linked list: \ n"); -Convert (A);//single linked list A reverse $ print (A); the}
2. Use the STL Template:
A. Using the queue operation, the original linked list is queued from the end of the queue, and then taken out from the queue head, the list of the head insert can be achieved.
1#include <iostream>2#include <list>3#include <algorithm>4#include <deque>//Queue5 6 using namespacestd;7 8list<int> Convert (list<int> IList)//single linked list reverse9 {Ten intlength; Onedeque<int>Ideque; ALength=ilist.size (); - for(intI=0; i<length;++i) -{Ideque.push_back (*Ilist.begin ()); the Ilist.pop_front (); - } -Length=ideque.size (); - for(i=0; i<length;++i) + { -Ilist.push_front (*Ideque.begin ()); + Ideque.pop_front (); A } at returnIList; - } - - voidPrint (list<int>IList) - { -list<int>:: iterator ite; in for(Ite=ilist.begin (); Ite!=ilist.end (); + +ite) -cout<<*ite<<" -"; tocout<<Endl; + } - the voidMain () * { $list<int>IList;Panax Notoginseng for(intI=1;i<Ten; i++) - Ilist.push_back (i); the print (IList); + print (CONVERT (IList)); A}
B. Using the stack operation, the original linked list from the head into the stack, and then removed from the top of the stack, the end of the list inserted can be achieved.
1#include <iostream>2#include <list>3#include <algorithm>4#include <stack>//Stack5 6 using namespacestd;7 8list<int> Convert (list<int> IList)//single linked list reverse9 {Ten intlength; Onestack<int>Istack; ALength=ilist.size (); - for(intI=0; i<length;++i) -{Istack.push (*Ilist.begin ()); the Ilist.pop_front (); - } -Length=istack.size (); - for(i=0; i<length;++i) + { - Ilist.push_back (Istack.top ()); + Istack.pop (); A } at returnIList; - } - - voidPrint (list<int>IList) - { -list<int>:: iterator ite; in for(Ite=ilist.begin (); Ite!=ilist.end (); + +ite) -cout<<*ite<<" -"; tocout<<Endl; + } - the voidMain () * { $list<int>IList;Panax Notoginseng for(intI=1;i<Ten; i++) - Ilist.push_back (i); the print (IList); + print (CONVERT (IList)); A}
Results:
Single-linked list reversal