为什么红色部分在2中能通过,并且正确.而在1中连编译都通不过?
程序1:实际一个将输入的数据建立成链表、输出链表数据,并在程序结束后释放.
#include <stdlib.h> #define Max 10 struct List { int Number; char Name; struct List *Next; }; typedef struct List Node; typedef Node *Link; void Free_List(Link Head) { Link Pointer; while(Head!=NULL) { Pointer=Head; Head=Head->Next; free(Pointer); } } void Print_List(Link Head) { Link Pointer; Pointer=Head; while(Pointer!=NULL) { printf("##Input Data##\n"); printf("Data Number:%d\n",Pointer->Number); printf("Data Name:%s\n",Pointer->Name); Pointer=Pointer->Next; } } Link Create_List(Link Head) { int DataNum; char DataName[Max]; Link New; Link Pointer; int i; Head=(New)=malloc(sizeof(Node)); if(Head==NULL) printf("Memory allocate Failure!!\n"); else { DataNum=1; printf("Please input the data name:"); scanf("%s",DataName); Head->Number=DataNum; for(i=0;i<=Max;i++) Head->Name[i]=DataName[i]; <-------------使用strcpy能通过
Head->Next=NULL; Pointer=Head; while(1) { DataNum++; New=(Link)malloc(sizeof(Node)); printf("Please input the data name:"); scanf("%s",DataName); if(DataName[0]=='0') break; New->Number=DataNum; for(i=0;i<=Max;i++) Head->Name[i]=DataName[i]; New->Next=NULL; Pointer->Next=New; Pointer=New; } } return Head; } void main() { Link Head; Head=Create_List(Head); if(Head!=NULL) { Print_List(Head); Free_List(Head); } }
程序2:设计一个节点的配置与释放的程序
#include <stdlib.h> #define Max 10 struct List { int Number; char Name[Max]; struct List *Next; };
typedef struct List Node; typedef Node *Link; void main() { Link New; int DataNum; char DataName[Max]; int i; New=(Link)malloc(sizeof(Node)); if(New==NULL) printf("Memory allocate Failure!!\n"); else { printf("Please input the data number:"); scanf("%d",&DataNum); printf("Please input the data name:"); scanf("%s",DataName); New->Number=DataNum; for(i=0;i<=Max;i++) New->Name[i]=DataName[i];
New->Next=NULL; printf("##Input Data##\n"); printf("Data Number:%d\n",New->Number); printf("Data Name:%s\n",New->Name); } free(New); }
[此贴子已经被作者于2004-11-17 21:59:52编辑过]