C语言。。。。

[复制链接]
查看11 | 回复4 | 2008-9-13 03:12:25 | 显示全部楼层 |阅读模式
#include
#include
struct node
{char info[5];
struct node *link;
};
main()
{struct node *create(char *);
void print(struct node *);
struct node *head=NULL
char c[]="the teacher";
head=create(c);
print(head);
}
struct node *create(char *s)
{int k;struct node *h,*p;
h=NULL;
while(*s)
{k=0;
p=(struct node *)malloc(sizeof(struct node ));
while(kinfo[k++]=*s++;
p->info[k]='\0';p->link=h;
h=p;
}
return h;
}
void print(struct node *head)
{struct node *p=head;
while(p!=NULL)
{puts(p->info);p=p->link;}
}
第1行是her
第2行是teach
第3行是the
怎么算的??
详细点
谢谢

回复

使用道具 举报

千问 | 2008-9-13 03:12:25 | 显示全部楼层
实际运行输出是这样: her teac the 代码分析: (1)先看这个函数 struct node *create(char *s) { int k;struct node *h,*p; h=NULL; while(*s) //如果字符串还没结束时继续 {k=0; p=(struct node *)malloc(sizeof(struct node )); //分配一个结点内存 while(kinfo[k++]=*s++; //每次取一个字符,同时指针向后移 p->info[k]='\0';p->link=h; //在取够4个...
回复

使用道具 举报

千问 | 2008-9-13 03:12:25 | 显示全部楼层
我也很不理解.也没有输出\n的地方,怎么会分行呢? #include #include struct node //定义结点 {int d;struct node *next;}; struct node *create(void) //建立链表的函数,返回值是struct node类型 {stru...
回复

使用道具 举报

千问 | 2008-9-13 03:12:25 | 显示全部楼层
第1行是her 第2行是tech 第3行是the (the后面还有一个空格) 这个程序就是将"the teacher"这个字符串以每4个字符拆分出来,倒序放到一个链表中,然后将链表各节点存放的子字符串打印出来 没有'\n'却换行是由于puts的原因,puts会将传入的字符串打印出来,同时会在最后加上换行符 c库里面只有stdlib...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行