从尾到头打印链表

来源:互联网 发布:sql培训机构 编辑:程序博客网 时间:2024/05/16 01:48
#include<stdio.h>#include<iostream>#include<stdlib.h>using namespace std;struct ListNode{int m_nKey;ListNode* m_pNext;};void print_list(ListNode* head){if(head==NULL)return;print_list(head->m_pNext);cout<<head->m_nKey<<" ";}ListNode* create_list(int n){ListNode* head;ListNode* p,* pre;int i;head=(ListNode*) malloc(sizeof(ListNode));head->m_pNext=NULL;pre=head;for(i=0;i<n;i++){p=(ListNode*)malloc(sizeof(ListNode));p->m_nKey=i;pre->m_pNext=p;pre=p;}p->m_pNext=NULL;return head;}int main(){ListNode* head=create_list(10);ListNode* next=head->m_pNext;print_list(next);return 0;}


0 0
原创粉丝点击