太阳能热水器最大容量:请帮忙解决这c程序问题,谢谢

来源:百度文库 编辑:神马品牌网 时间:2024/05/02 17:25:31
输入若干人的姓名,以#作为结束标志,将所输入的名字如入到链表中
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
struct list
{
char name[16];
struct list *next;
};

typedef struct list node;
typedef node *link ;
main()
{
link ptr,head,chang;
char name1[16];
int end=0;
head=(link)malloc(sizeof(node));
ptr=head;
printf("please input name ==>\n");
while(1)
{ gets(name1);
end=strcmp(name1,"#")==0;
if(end)break;
ptr->name[16]=name1;
chang=(link)malloc(sizeof(node));
ptr->next=chang;
ptr=chang;
}
if(end)
ptr->next=NULL;
ptr=head;
while(ptr!=NULL)
{
printf("the name is==>%s\n",ptr->name);/*可是总是不能够显示输进去的name */
ptr=ptr->next;
}
getch();
}

试下把
ptr->name[16]=name1;

改为:
strcpy(ptr->name, name1);

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>

struct list
{
char name[16];
struct list *next;
};

typedef struct list node;
typedef node *link ;

main()
{
link ptr,head,chang;
char name1[16];
int end=0;
head=(link)malloc(sizeof(node));
ptr=head;
printf("please input name ==>\n");
while(1)
{
gets(name1);
end=strcmp(name1,"#")==0;
if(end)break;
strcpy(ptr->name,name1);
chang=(link)malloc(sizeof(node));
ptr->next=chang;
ptr=chang;
}
if(end)
ptr->next=NULL;
ptr=head;
while(ptr->next!=NULL)
{
printf("the name is==>%s\n",ptr->name);
ptr=ptr->next;
}
getch();
}
试下,在VC.NET下通过。