1 条题解

  • 0
    @ 2025-4-7 21:28:48

    C :

    #include <stdio.h>
    #include <stdlib.h>
    /* 结构体定义 */
    struct student {
    	long num;
    	float score;
    	struct student * next;
    };
    /* n是全局变量,表示当前动态链表的节点个数 */
    int n;
    /* 定义建立动态链表的函数,返回值为指向链表头的指针 */
    struct student * create(void) {
    	struct student * head, *tail, *p;
    	n = 0;
    	head = NULL;
    	tail = p = (struct student *)malloc(sizeof(struct student));
    	scanf("%ld %f", &p->num, &p->score);
    	while (p->num != 0) {
    		n++;
    		if (n == 1)
    			head = p;
    		else
    			tail->next = p;
    		tail = p;
    		p = (struct student *)malloc(sizeof(struct student));
    		scanf("%ld %f", &p->num, &p->score);
    	}
    	tail->next = NULL;
    	free(p);
    	return head;
    }
    /* 定义输出链表的函数 */
    void print(struct student * head) {
    	struct student * p;
    	p = head;
    	printf("%d\n", n);
    	while (p != NULL) {
    		printf("%ld %.1f\n", p->num, p->score);
    		p = p->next;
    	}
    }
    /* 定义删除特定学号节点的函数,返回值为指向链表头的指针 */
    struct student * del(struct student * head, long num) {
    	struct student *p1, *p2;
    	if (head == NULL)
    		return head;
    	p1 = head;
    	/* 如果p1指向的不是要找的节点且后面还有节点,则p1后移 */
    	while (p1->num != num && p1->next != NULL) {
    		p2 = p1;
    		p1 = p1->next;
    	}
    	/* 找到了需要的节点 */
    	if (num == p1->num) {
    		/* 如果需要的节点是首节点,则首节点后移;
    		否则将下一节点的地址复制给前一节点的next */
    		if (p1 == head)
    			head = p1->next;
    		else
    			p2->next = p1->next;
    		free(p1);
    		n--;
    	}
    	return head;
    }
    /* 定义插入节点的函数,返回值为指向链表头的指针 */
    struct student * insert(struct student * head, struct student * stud) {
    	struct student *p1, *p2;
    	p1 = head;
    	/* 若原来的链表是空的,则使插入的节点为头节点 */
    	if (head == NULL) {
    		head = stud;
    		head->next = NULL;
    	} else {
    		/* 如果p1指向的节点学号小于要插入的且后面还有节点,则p1后移 */
    		while (p1->num < stud->num && p1->next != NULL) {
    			p2 = p1;
    			p1 = p1->next;
    		}
    		/* 新节点要插入在p1之前 */
    		if (p1->num > stud->num) {
    			/* 需要插入在头结点之前的特殊情况 */
    			if (p1 == head) {
    				head = stud;
    				head->next = p1;
    			} else {
    				p2->next = stud;
    				stud->next = p1;
    			}
    		} else {
    			/* 新节点要插入在p1之后 */
    			p1->next = stud;
    			stud->next = NULL;
    		}
    	}
    	n++;
    	return head;
    }
    /* 主函数 */
    int main() {
    	struct student *head, *stud;
    	long num_to_del;
    	head = create();
    	print(head);
    	scanf("%ld", &num_to_del);
    	while (num_to_del != 0) {
    		head = del(head, num_to_del);
    		print(head);
    		scanf("%ld", &num_to_del);
    	}
    	stud = (struct student *)malloc(sizeof(struct student));
    	scanf("%ld %f", &stud->num, &stud->score);
    	while (stud->num != 0) {
    		head = insert(head, stud);
    		print(head);
    		stud = (struct student *)malloc(sizeof(struct student));
    		scanf("%ld %f", &stud->num, &stud->score);
    	}
    	free(stud);
    	return 0;
    }
    
    

    C++ :

    #include <stdio.h>
    #include <stdlib.h>
    /* 结构体定义 */
    struct student {
    	long num;
    	float score;
    	struct student * next;
    };
    /* n是全局变量,表示当前动态链表的节点个数 */
    int n;
    /* 定义建立动态链表的函数,返回值为指向链表头的指针 */
    struct student * create(void) {
    	struct student * head, *tail, *p;
    	n = 0;
    	head = NULL;
    	tail = p = (struct student *)malloc(sizeof(struct student));
    	scanf("%ld %f", &p->num, &p->score);
    	while (p->num != 0) {
    		n++;
    		if (n == 1)
    			head = p;
    		else
    			tail->next = p;
    		tail = p;
    		p = (struct student *)malloc(sizeof(struct student));
    		scanf("%ld %f", &p->num, &p->score);
    	}
    	tail->next = NULL;
    	free(p);
    	return head;
    }
    /* 定义输出链表的函数 */
    void print(struct student * head) {
    	struct student * p;
    	p = head;
    	printf("%d\n", n);
    	while (p != NULL) {
    		printf("%ld %.1f\n", p->num, p->score);
    		p = p->next;
    	}
    }
    /* 定义删除特定学号节点的函数,返回值为指向链表头的指针 */
    struct student * del(struct student * head, long num) {
    	struct student *p1, *p2;
    	if (head == NULL)
    		return head;
    	p1 = head;
    	/* 如果p1指向的不是要找的节点且后面还有节点,则p1后移 */
    	while (p1->num != num && p1->next != NULL) {
    		p2 = p1;
    		p1 = p1->next;
    	}
    	/* 找到了需要的节点 */
    	if (num == p1->num) {
    		/* 如果需要的节点是首节点,则首节点后移;
    		否则将下一节点的地址复制给前一节点的next */
    		if (p1 == head)
    			head = p1->next;
    		else
    			p2->next = p1->next;
    		free(p1);
    		n--;
    	}
    	return head;
    }
    /* 定义插入节点的函数,返回值为指向链表头的指针 */
    struct student * insert(struct student * head, struct student * stud) {
    	struct student *p1, *p2;
    	p1 = head;
    	/* 若原来的链表是空的,则使插入的节点为头节点 */
    	if (head == NULL) {
    		head = stud;
    		head->next = NULL;
    	} else {
    		/* 如果p1指向的节点学号小于要插入的且后面还有节点,则p1后移 */
    		while (p1->num < stud->num && p1->next != NULL) {
    			p2 = p1;
    			p1 = p1->next;
    		}
    		/* 新节点要插入在p1之前 */
    		if (p1->num > stud->num) {
    			/* 需要插入在头结点之前的特殊情况 */
    			if (p1 == head) {
    				head = stud;
    				head->next = p1;
    			} else {
    				p2->next = stud;
    				stud->next = p1;
    			}
    		} else {
    			/* 新节点要插入在p1之后 */
    			p1->next = stud;
    			stud->next = NULL;
    		}
    	}
    	n++;
    	return head;
    }
    /* 主函数 */
    int main() {
    	struct student *head, *stud;
    	long num_to_del;
    	head = create();
    	print(head);
    	scanf("%ld", &num_to_del);
    	while (num_to_del != 0) {
    		head = del(head, num_to_del);
    		print(head);
    		scanf("%ld", &num_to_del);
    	}
    	stud = (struct student *)malloc(sizeof(struct student));
    	scanf("%ld %f", &stud->num, &stud->score);
    	while (stud->num != 0) {
    		head = insert(head, stud);
    		print(head);
    		stud = (struct student *)malloc(sizeof(struct student));
    		scanf("%ld %f", &stud->num, &stud->score);
    	}
    	free(stud);
    	return 0;
    }
    
    
    • 1

    信息

    ID
    1380
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者