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;其中代码改成头插法怎么改
时间: 2024-03-23 08:37:21 浏览: 90
头插法--建立字母链表.opt
将链表节点插入到链表头部的操作称为头插法,可以将新节点插入到链表头部,然后让新节点的next指向原来的头节点。头插法的代码实现如下:
```c
void Create(LinkList &Head, int score, int rank, int sex, char name[], char college[], char item[]) {
LinkNode *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 = Head->next;
Head->next = s;
}
```
相比于原来的代码,头插法只需在新节点的next指针赋值时,将其指向原来的头节点即可,同时不需要遍历链表找到末尾节点。
阅读全文