根据P257页例题13-8要求,编写一个链表结构,读取任意多个用户输入。之后删除学号为2017003的节点信息,并把删除后的链表打印出来
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct node_s {
long num;
float score;
struct node_s* next;
} node_t;
int n;
node_t* create()
{
node_t *head, *p1, *p2;
n = 0;
p1 = p2 = (node_t*) malloc(sizeof(node_t));
scanf("%ld %f", &p1->num, &p1->score);
head = NULL;
while (p1->num != 0) {
n++;
if (n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (node_t*) malloc(sizeof(node_t));
scanf("%ld %f", &p1->num, &p1->score);
}
p2->next = NULL;
return (head);
}
void print(node_t *head)
{
node_t *p;
if (head == NULL) {
return;
}
for (p = head; p != NULL; p = p->next) {
printf("%ld %5.1f\n", p->num, p->score);
}
}
node_t *find(node_t *head, int num)
{
node_t *p;
if (head == NULL) {
return NULL;
}
for (p = head; p != NULL; p = p->next) {
if(p->num == num){
return p;
}
}
return NULL;
}
node_t *del(node_t *head, int num)
{
node_t *p1, *p2;
if (head == NULL) {
printf("list is NULL\n");
return head;
}
for (p1 = head; p1->next != NULL; p2=p1, p1=p1->next) {
if(num == p1->num){
break;
}
}
if (num == p1->num) {
if (p1 == head) {
head = p1->next;
} else {
p2->next = p1->next;
}
free(p1); //书上漏了释放内存
p1 = NULL;
n--;
return head;
} else {
printf("not exist: num %d\n", num);
return head;
}
}
node_t *insert(node_t *head, node_t *stud)
{
node_t *p0, *p1, *p2;
p1 = head;
p0 = stud;
//创建首节点
if (head == NULL) {
head = p0;
p0->next = NULL;
n++;
return head;
}
//找到插入位置
for (p1 = head; p1->next != NULL; p2=p1, p1 = p1->next) {
if(p0->num < p1->num) {
break;
}
}
if (p0->num <= p1->num) {
if (p1 == head) { //找到插入位置,插入队首
head = p0;
p0->next = p1;
} else { //找到插入位置,插入队列中间位置
p2->next = p0;
p0->next = p1;
}
} else {//找不到插入位置,插入队尾
p1->next = p0;
p0->next = NULL;
}
n++;
return head;
}
//输出
void test_14_1(void)
{
node_t* head;
head = create();
print(head);
}
//查找
void test_14_2(void)
{
node_t* head;
node_t* ret;
head = create();
ret = find(head, 2017003);
if (ret) {
printf("%ld %0.1f\n", ret->num, ret->score);
} else {
printf("%p\n", ret);
}
}
//插入
node_t* test_14_3(void)
{
node_t* head;
node_t* stud;
head = create();
//插入头部
stud = (node_t*) malloc(sizeof(node_t));
stud->num = 2017000;
stud->score = 85.0;
head = insert(head, stud);
//插入中间
stud = (node_t*) malloc(sizeof(node_t));
stud->num = 2017006;
stud->score = 85.0;
head = insert(head, stud);
//插入尾部
stud = (node_t*) malloc(sizeof(node_t));
stud->num = 2017013;
stud->score = 85.0;
head = insert(head, stud);
print(head);
return head;
}
//删除
void test_14_4(void)
{
node_t* head;
head = create();
head = del(head, 2017001);
printf("delete num %d\n", 2017001);
head = del(head, 2017005);
printf("delete num %d\n", 2017005);
head = del(head, 2017010);
printf("delete num %d\n", 2017010);
print(head);
}
//删除
void test_14_5(void)
{
node_t* head;
head = create();
head = del(head, 2017003);
print(head);
}
#define T5
/*
2017001 81.0
2017002 82.0
2017003 83.0
2017004 84.0
2017005 85.0
2017010 85.0
0 0
*/
int main(void)
{
#ifdef T1
test_14_1();
#elif defined(T2)
test_14_2();
#elif defined(T3)
test_14_3();
#elif defined(T4)
test_14_4();
#elif defined(T5)
test_14_5();
#endif
//system("pause");
}
输入
2017001 81.0
2017002 82.0
2017003 83.0
2017004 84.0
2017005 85.0
2017010 85.0
0 0
输出
2017001 81.0
2017002 82.0
2017004 84.0
2017005 85.0
2017010 85.0