The first type:
First, reverse the list, and then visit.
(1) Pick node, change the original linked list structure
(2) Application space
#include <stdio.h> #include <stdlib.h>typedef int Datatype;typedef struct Listnode{datatype _data;struct listnode* _next;} ListNode; Listnode* buynode () {listnode* tmp= (listnode*) malloc (sizeof (ListNode)); return tmp;} Void push (listnode* &head,datatype data) {listnode* cur=head;if (Cur==NULL) {Cur=Buynode ( ); cur->_data=data;cur->_next=null;head=cur;} Else{while (cur->_next !=null) {cur=cur->_next;} Listnode* tmp=null;tmp=buynode (); tmp->_data=data;tmp->_next=null;cur->_next=tmp;}} Void pop (Listnode* head) {listnode* cur=head; Listnode* prev=null;if (cur==null) { return ;} while (cur->_next!=null) {prev=cur;cur=cur->_next;} Delete cur;prev->_next=null;cur=null;} Void reverse (Listnode* head) {listnode* cur=head; listnode* tmp=null; Listnode* newhead=null;while (cur) {tmp=cur;cur=cur->_next;tmp->_next=Newhead;newhead=tmp;} while (Newhead) {printf ("%d\n", newhead->_data); newhead=newhead->_next;}} Int main () {listnode* head=null; Push (head,1); Push (head,2); Push (head,3); Push (head,4); Push (head,5); reverse (head); System ("pause"); return 0;}
The second type:
Using the advanced post-out characteristics of the stack, loop through the linked list, sequentially put the linked list data into the stack, and then traverse the stack
#include <iostream> #include <stack>using namespace std;template<class t>struct Listnode{Listnode<T> (t data=0): _data (data), _next (NULL) {}t _data; listnode<t>* _next;}; Template<class t>class list{public:list (listnode<t>* head=null): _head (head) {}~List ( ) {listnode<t>* cur=_head;while (cur) {listnode<t>* tmp=cur;cur=cur->_next;delete tmp;}} Void push (t data) {listnode<t>* cur=_head;if (cur==null) { cur=new listnode<t > (data); _head=cur;} Else{while (cur->_next!=null) {cur=cur->_next;} cur->_next=new listnode<t> (data);}} Void pop () {listnode<t>* cur=_head;if (cur==null) {return;} Listnode<t>* prev=null;while (cur->_next!=null) {prev=cur;cur=cur->_next;} Delete cur;prev->_next=null;cur=null;} Void reverorderprint () {stack<int> s; Listnode<t>* cur=_head;while (cur) {S.push (cUr->_data); cur=cur->_next;} Int size=s.size (); while (size--) {cout<<s.top () << ' '; S.pop ();}} private:listnode<t> *_head;}; Void test () {List<int> l;l.push (1); L.push (2); L.push (3); L.push (4); L.push (5); L.reverorderprint ();} Int main () {test (); System ("pause"); return 0;}
Third type: recursive implementation
#include <stdio.h> #include <stdlib.h> #include <string.h>typedef int datatype;typedef struct listnode{datatype _data;struct listnode* _next;} ListNode; Listnode* buynode () {listnode* tmp= (listnode*) malloc (sizeof (ListNode)); return tmp;} Void push (listnode* &head,datatype data) {listnode* cur=head;if (Cur==NULL) {Cur=Buynode ( ); cur->_data=data;cur->_next=null;head=cur;} Else{while (cur->_next !=null) {cur=cur->_next;} Listnode* tmp=null;tmp=buynode (); tmp->_data=data;tmp->_next=null;cur->_next=tmp;}} Void pop (Listnode* head) {listnode* cur=head; Listnode* prev=null;if (cur==null) { return ;} while (cur->_next!=null) {prev=cur;cur=cur->_next;} Delete cur;prev->_next=null;cur=null;} Void revorderprint (listnode* head) {listnode* cur=head;if (cur) {revorderprint (cur->_next); printf ("%d\n", Cur->_data);} Else{rEturn;}} Int main () {listnode* head=null; Push (head,1); Push (head,2); Push (head,3); Push (head,4); Push (head,5); revorderprint (head); System ("pause"); return 0;}
Enter the head node of a linked list, which in turn prints the value of each node from the end of the head.