#include<stdio.h>
#include<malloc.h>
struct snode
{
char name[15];
struct snode*next;
};
struct snode*create_list(int n)
{
int i;
struct snode*head,*tail,*p;
head=tail=NULL;
printf(“请输入姓名:\n”);
for(i=0;i<n;i++)
{
p=(struct snode*)malloc(sizeof(struct snode)); /*为节点分配空间*/
scanf("%s",p->name);
p->next=NULL; /*新加入的节点链入表尾,故其next指针均置为NULL*/
if(head==NULL) /*第一个节点*/
head=tail=p; /*只有一个节点时,head和tail均指向它*/
else
{
tail->next=p; /*当前尾节点的next指针指向新节点*/
tail=p; /*尾指针向后移指向新节点*/
}
}
return head;
}
main()
{
struct snode*q;
q=create_list(5); /*创建一个链表,包含5个节点*/
printf(“生成的字符串链表为:\n”);
while(q!=NULL)
{
printf("%s\n",q->name);
q=q->next;
}
}