首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

(请问)一道建立链表和输出的填空题

2012-03-04 
(请教)一道建立链表和输出的填空题!下面程序的功能是:从键盘输入多行字符串,调用函数建立反序链表,然后输

(请教)一道建立链表和输出的填空题!
下面程序的功能是:从键盘输入多行字符串,调用函数建立反序链表,然后输出整个链表。
#include <stdio.h>
struct   node
{char   data[80];
struct   node   *link;
}*head;
ins   (struct   node[4])
{if   (head==NULL)
{q-> link=NULL;   head=q;}
else
{[5];}
}
main()
{char   *ch;
struct   node   *p;
head=NULL;
while(strlen(gets(ch))> 0)
{p=([6])malloc(sizeof(struct   node));
strcpy(p-> data,ch);
ins([7]);}
p=head;
while([8])
{printf(“%s\n”,p-> data);
p=p-> link;}
}
请问4,5,6,7,8空应填什么,最好能解释一下?

[解决办法]
#include <stdio.h>
struct node
{char data[80];
struct node *link;
}*head;
ins (struct node *q)
{if (head == NULL)
{q-> link = NULL;head = q;}
else
{q = head-> link;
head =q;
}
main()
{char *ch;
struct node *p;
head = NULL;
while(strlen(gets(ch))> 0)
{p = (struct node *) malloc(sizeof(struct node);
strcpy(p-> data,ch);
ins(p);
p=head;
while(p!=NULL)
{printf( "%s\n ",p-> data);
p=p-> link;}
}
[解决办法]
//小改了一下,可以完全运行,如下所示:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct node
{
char data[80];
struct node *link;
}*head;

void ins(struct node * q) //4
{
if (head == NULL)
{
q-> link = NULL;
head = q;
}
else
{
q-> link = head; //5
head = q;
}
}

void main()
{
char ch[80];
struct node *p;
head = NULL;
printf( "input the str onr by one: \n ");
while( strlen(gets(ch)) > 0)
{
p = (struct node *)malloc(sizeof(struct node)); //6
strcpy(p-> data, ch);
ins(p); //7
printf( "input the another one: \n ");
}
p = head;

printf( "the total output: \n ");
while(p != NULL) //8
{
printf( "%s\n ",p-> data);
p = p-> link;
}

}

热点排行