C语言单向链表操作:创建和删除低于分数线节点
#include <stdio.h>
#include <stdlib.h>
struct stud_node {
int num; /*学号*/
char name[20]; /*姓名*/
int score; /*成绩*/
struct stud_node *next; /*指向下个结点的指针*/
};
struct stud_node *createlist() {
struct stud_node *head, *tail, *p;
head = (struct stud_node*)malloc(sizeof(struct stud_node));
tail = head;
scanf('%d', &tail->num);
while (tail->num) {
scanf('%s%d', tail->name, &tail->score);
p = (struct stud_node*)malloc(sizeof(struct stud_node));
tail->next = p;
tail = p;
scanf('%d', &tail->num);
}
tail->next = NULL;
return head;
}
struct stud_node *deletelist(struct stud_node *head, int min_score) {
struct stud_node *p = head, *q;
while (p != NULL && p->score < min_score) {
head = p->next;
free(p);
p = head;
}
while (p != NULL) {
while (p->next != NULL && p->next->score < min_score) {
q = p->next;
p->next = q->next;
free(q);
}
p = p->next;
}
return head;
}
int main() {
int min_score;
struct stud_node *p, *head = NULL;
head = createlist();
scanf('%d', &min_score);
head = deletelist(head, min_score);
for ( p = head; p != NULL; p = p->next )
printf('%d %s %d\n', p->num, p->name, p->score);
return 0;
}
输入样例:
1 zhang 78 2 wang 80 3 li 75 4 zhao 85 0 80
输出样例:
2 wang 80 4 zhao 85
原文地址: https://www.cveoy.top/t/topic/nydU 著作权归作者所有。请勿转载和采集!