[求助]建立包含N个人姓名的单链表的问题
#include <stdio.h>#include <conio.h>
#include "string.h"
#include <malloc.h>
#define SIZE 2
#define NULL 0
struct node
{
char name[SIZE];
struct node *next ;
};
node *creat()
{
node *head,*p,*q;
int i=0;
char s[100];
head=(node *)malloc(sizeof(node));
head->next=NULL;
q=head;
printf("please input your data:\n");
fflush(stdin);
while(i<SIZE)
{
p=(node *)malloc(sizeof(node));
gets(s);
strcpy(p->name,s);
p->next=NULL;
q->next=p;
q=p;
i++;
}
return head;
}
void disp(node *head)
{
node *p;
p=head;
printf("print the data:\n");
while(p!=NULL)
{
printf("%s",p->name);
p=p->next;
printf("\n");
}
}
int main()
{
node *head;
head=creat();
disp(head);
getch();
return(0);
}