第1个回答
Midas2017.09.27回答可以使用递归实现。以栈的形式,从最后一次调用函数开始,输出最后一个值,然后依次向前回退输出。以下为示例代码:#include<stdio.h>#include<malloc.h>structnode{intdata;structnode*next;};voidrprint(structnode*head);structnode*createNode(intlen);intmain(){structnode*head;head=createNode(10);//创建一个长度为10的单链表printf("------output:------\n");rprint(head);//逆序输出return0;}//实现逆序输出的函数voidrprint(structnode*head){if(head==NULL){return;}else{rprint(head->next);printf("%d\n",head->data);}}//创建链表,len为链表长度structnode*createNode(intlen){structnode*head=NULL,*p1=NULL,*p2=NULL;inti;head=p1=p2=(structnode*)malloc(sizeof(structnode));for(i=0;i<len;i++){p1=p2;scanf("%d",&(p2->data));p2=(structnode*)malloc(sizeof(structnode));p1->next=p2;}p1->next=NULL;//如果长度为空if(len==0){head=NULL;}returnhead;}测试输出成功: