本题要求实现两个函数一个将输入的学生成绩组织成单向链表;另一个将成绩低于某分数线的学生结点从链表中删除。函数接口定义:struct stud_node createlist;struct stud_node deletelist struct stud_node head int min_score ;函数createlist利用scanf从输入中获取学生的信息将其组织成单向链表并返回链表头指针。
struct stud_node *createlist() { struct stud_node *head = NULL, *tail = NULL; int num, score; char name[20]; while (scanf("%d", &num) == 1 && num != 0) { scanf("%s %d", name, &score); struct stud_node p = (struct stud_node)malloc(sizeof(struct stud_node)); p->num = num; strcpy(p->name, name); p->score = score; p->next = NULL; if (!head) { head = tail = p; } else { tail->next = p; tail = p; } } return head; }
struct stud_node *deletelist(struct stud_node *head, int min_score) { struct stud_node dummy = (struct stud_node)malloc(sizeof(struct stud_node)); dummy->next = head; struct stud_node *p = dummy; while (p->next) { if (p->next->score < min_score) { struct stud_node *q = p->next; p->next = q->next; free(q); } else { p = p->next; } } return dummy->next;
原文地址: https://www.cveoy.top/t/topic/cFIz 著作权归作者所有。请勿转载和采集!