void Create(LinkList &Head, int score, int rank, int sex, char name[], char college[], char item[]) { LinkNode *p = Head->next, *s; //如果链表为空,则做初始化链表 if (p == NULL) { s = (LinkNode *)malloc(sizeof(LinkNode)); assert(s != NULL); s->score = score; s->rank = rank; s->sex = sex; strcpy(s->name, name); strcpy(s->college, college); strcpy(s->item, item); s->next = NULL; Head->next = s; } else { while (p->next != NULL) { p = p->next; } s = (LinkNode *)malloc(sizeof(LinkNode)); assert(s != NULL); s->score = score; s->rank = rank; s->sex = sex; strcpy(s->name, name); strcpy(s->college, college); strcpy(s->item, item); s->next = NULL; p->next = s; } }
时间: 2024-03-23 10:37:20 浏览: 71
这是一个函数,用于向链表中添加节点(学生信息),函数的参数包括链表头指针Head,学生的成绩score,名次rank,性别sex,姓名name,学院college和项目item。函数的具体实现如下:
1. 首先判断链表是否为空,如果为空则新建一个节点作为头节点,并将新节点作为第一个节点。
2. 如果链表不为空,则遍历链表,找到链表的末尾节点,将新节点添加在其后面。
3. 新建节点时,需要动态分配内存空间,判断是否分配成功。
4. 将学生的信息存储到节点中,包括成绩、名次、性别、姓名、学院和项目。
5. 将新节点添加到链表末尾,即链表尾部节点的next指针指向新节点。
函数的完整代码如下:
```c
void Create(LinkList &Head, int score, int rank, int sex, char name[], char college[], char item[]) {
LinkNode *p = Head->next, *s;
// 如果链表为空,则做初始化链表
if (p == NULL)
{
s = (LinkNode *)malloc(sizeof(LinkNode));
assert(s != NULL);
s->score = score;
s->rank = rank;
s->sex = sex;
strcpy(s->name, name);
strcpy(s->college, college);
strcpy(s->item, item);
s->next = NULL;
Head->next = s;
}
else
{
while (p->next != NULL)
{
p = p->next;
}
s = (LinkNode *)malloc(sizeof(LinkNode));
assert(s != NULL);
s->score = score;
s->rank = rank;
s->sex = sex;
strcpy(s->name, name);
strcpy(s->college, college);
strcpy(s->item, item);
s->next = NULL;
p->next = s;
}
}
```
阅读全文