习题 9.7 在第9章例9.9和例9.10的基础上,写一个函数del,用来删除动态链表中指定的结点。

C程序设计(第四版) 谭浩强 习题9.7 个人设计

习题 9.7 在第9章例9.9和例9.10的基础上,写一个函数del,用来删除动态链表中指定的结点。

代码块:

由于链表是动态链表,不能按照结构体变量为数组的时候通过指针增减调整指针指向,借鉴网上的提示,将原来的单链表增加一个结构体内指针,成为双向链表。

方法1:

#include 
#include 
struct Student{
    int num;
    float score;
    Student *prev;                      //此处为增加的指向上一个结构体变量的指针
    Student *next;
};
Student *input();                       //定义输入函数
void print(Student *stu);               //定义输出函数
Student *del(Student *stu, float n);    //定义删除函数
int main()
{
    Student *stud;                      //定义结构体变量并输入动态链表
    stud=input();
    print(del(stud, 99));               //调用输出函数,参数为删除函数返回的指针值
    system("pause");
    return 0;
}
//输入函数
Student *input()
{
    Student *p1, *p2, *head;
    int n=0;
    p1=p2=(Student*)malloc(sizeof(Student));
    printf("Please enter No.%d student info: ", n+1);
    scanf("%d %f", &p1->num, &p1->score);
    head=NULL;
    while (p1->num!=0){
        n++;
        if (n==1){
            head=p1;
            p1->prev=NULL;                     //在原例题基础上,将上一个指针指向上一个结构体指针
        }
        else{
            p2->next=p1;
            p1->prev=p2;
        }
        p2=p1;
        p1=(Student*)malloc(sizeof(Student));
        printf("Please enter No.%d student info: ", n+1);
        scanf("%d %f", &p1->num, &p1->score);
    }
    p2->next=NULL;
    return head;
}
//输出函数
void print(Student *stu)
{
    Student *p;
    for (p=stu; p!=NULL; p=p->next)
        printf("%d %.2f\n", p->num, p->score);
}
//删除函数
Student *del(Student *stu, float n)
{
    Student *p, *head;
    for (p=stu, head=p; p!=NULL; p=p->next)
        if (p->score==n){
            if (head==p)
                head=p->next;
            else if (p->next==NULL)
                p->prev->next=NULL;
            else
                p->prev->next=p->next;
        }
    return head;
}

方法2:

#include 
#include 
struct Student{
    int num;
    float score;
    Student *prev;
    Student *next;
};
Student *input();
void print(Student *stu);
void del(Student *stu, float n);
int main()
{
    Student *stud;
    float del_score;
    stud=input();
    printf("Please enter delete score: ");             //从主函数中输入要删除的数据
    scanf("%f", &del_score);
    del(stud, del_score);
    print(stud);
    system("pause");
    return 0;
}
Student *input()
{
    Student *p1, *p2, *head;
    int n=0;
    p1=p2=(Student*)malloc(sizeof(Student));
    printf("Please enter No.%d student info: ", n+1);
    scanf("%d %f", &p1->num, &p1->score);
    head=NULL;
    while (p1->num!=0){
        n++;
        if (n==1){
            head=p1;
            p1->prev=NULL;
        }
        else{
            p2->next=p1;
            p1->prev=p2;
        }
        p2=p1;
        p1=(Student*)malloc(sizeof(Student));
        printf("Please enter No.%d student info: ", n+1);
        scanf("%d %f", &p1->num, &p1->score);
    }
    p2->next=NULL;
    return head;
}
void print(Student *stu)
{
    Student *p;
    for (p=stu; p!=NULL; p=p->next)
        printf("%d %.2f\n", p->num, p->score);
}
void del(Student *stu, float n)
{
    Student *p, *head;
    for (p=stu, head=p; p!=NULL; p=p->next)
        if (p->score==n){
            if (p==head)
                head=p->next;
            else if (p->next==NULL)
                p->prev->next=NULL;
            else
                p->prev->next=p->next;
        }
    *stu=*head;
}

你可能感兴趣的:(C程序设计,(第四版),谭浩强,课后答案)