以下是引用TIMFannie在2018-1-6 20:49:09的发言:
struct date{int year; int month; int day;}; //日期结构体类型
struct student //结构体类型
{ char name[10]; //人名
struct date birth; //出生日期
};
结构体数组s存储了n个人的名字和出生日期。写一函数,由数组s中n个人
的信息及其顺序构造相应的链表。链表的结点的结构体类型定义如下:
struct studentNode //结构体类型
{ char name[10]; //人名
struct date birth; //出生日期
struct studentNode *next
}
就这些了
改了一下 studentNode 结构表达形式,实质没变
#include <stdio.h>
#include <stdlib.h>
struct date
{
int year;
int month;
int day;
};
struct student
{
char name[10];
struct date birth;
};
struct studentNode
{
struct student st;
struct studentNode *next;
};
struct studentNode *CreateLinkList(struct student s[], int n)
{
struct studentNode *head=NULL, *p;
while (n--)
{
p = (struct studentNode *)malloc(sizeof(struct studentNode));
p->st = s[n];
p->next = head;
head = p;
}
return head;
}
void _free(struct studentNode *h)
{
struct studentNode *p;
while (h)
{
p = h->next;
free(h);
h = p;
}
}
void _prn(struct studentNode *h)
{
for (; h; h=h->next)
printf("%s %d %d %d\n", h->st.name, h->st.birth.year, h->st.birth.month, h->st.birth.day);
}
main()
{
struct student s[3]=
{
"a1",2011,1,11,
"a2",2012,2,12,
"a3",2013,3,13
};
struct studentNode *head = CreateLinkList(s, 3);
_prn(head);
_free(head);
}