#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
struct NoDe
{
int sorce;
char name[10];
struct NoDe * pNext;
};
typedef struct NoDe NODE, *PNODE;
PNODE CreatList(void);
void TraversalList(PNODE);
int main(void)
{
PNODE pHead;
pHead = CreatList();
TraversalList(pHead);
return 0;
}
PNODE CreatList(void)
{
int score, len, i;
char name[10];
PNODE pHead = NULL;
PNODE pNew = NULL;
PNODE pTail = NULL;
pHead = (PNODE)malloc(sizeof(NODE));
pTail = pHead;
printf("需要创建的节点个数:");
scanf("%d", &len);
for (i = 0; i < len; ++i)
{
pNew = (PNODE)malloc(sizeof(NODE));
if (NULL == pNew)
{
printf("内存分配失败");
exit(-1);
}
printf("请输入每个节点的信息,成绩:");
scanf("%d", &score);
printf("姓名:");
getchar();
gets(name);
pNew->sorce = score;
strcpy(pNew->name, name);
pTail->pNext = pNew;
pNew->pNext = NULL;
pTail = pNew;
}
return pHead;
}
void TraversalList(PNODE pHead)
{
PNODE p = pHead->pNext;
while (NULL != p)
{
printf("成绩为:%5d, 姓名为%s", p->sorce, p->name);
p = p->pNext;
}
}
|