用c语言实现单链表的逆序输出

 
 
<span style="font-family: Arial, Helvetica, sans-serif;">可以用递归,如果没到链表尾,则递归查询,否则输出当前值。下面只是算法表示,不能直接放到程序里编译执行。</span><span style="font-family: Arial, Helvetica, sans-serif;"> </span>
 
 
 
 
int outlink(node *p)
{
    if(p->next!=null)
        outlink(p->next);
   printf(p->data);
   return 0;
}
以下为代码实现:
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include<malloc.h>
struct String{ //字符串结构体
 char c;
 struct String *next;
};
void iniString(struct String *str){ //字符串初始化
 struct String *p1,*p2;
 p1=str;
 scanf("%c",&(p1->c));
 while(p1->c!='\n'){
  p2=(struct String *)malloc(sizeof(struct String *));
  scanf("%c",&(p2->c));
  p1->next=p2;
  p1=p1->next;
 }
 p1->next=NULL;
}
void strPrint(struct String *str){ //字符串链表逆序输出
 struct String *s=str;
 if(s->next!=NULL)
 {
  strPrint(s->next);
  printf("%c",s->c);
 }else
 {
  printf("%c",s->c);
 }
}
int main(){
 printf("请输入字符串(回车结束输入):");
 struct String str1;
 struct String *pstr1;
 pstr1=&str1;
 iniString(pstr1);
 strPrint(pstr1);
 system("pause");
 return 0;
}
http://zhidao.baidu.com/link?url=ixHSZqmxacDynyM8kT9ERRALx_NX3uqm6OwAnd0Fydnb9eLjlchSsFtpuHY999P1P48q3oBldWkpqD7qHNBko_

猜你喜欢

转载自blog.csdn.net/liuxiao19890212/article/details/38903387