isolation 发表于 2018-11-15 12:06:09

关于删除链表的程序

麻烦大家帮我看一下为啥程序还没走完就结束了,还没到删除节点程序就结束了。






#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#define LEN sizeof(struct student)

struct student *creat();
void print(struct student *head);
struct student *del(struct student *head,int num);

struct student
{
        int num;
        float score;
        struct student *next;
};
int n;

void main()
{       
        struct student *stu;
        int num;
        stu = creat();
        print(stu);
        printf("input the number you wanna delete: ");
        scanf("%d",&num);
        stu = del(stu,num);
        print(stu);
        printf("\n\n");
        system("pause");
}
struct student *creat()
{
        struct student *head,*p1,*p2;
        p1 = p2 = (struct student *)malloc(LEN);
        printf("please input the num:");
        scanf("%d",&p1->num);
        printf("please input the score:");
        scanf("%f",&p1->score);
       
        n = 0;
        while(p1->num != 0)
        {
                n++;
                if(n==1)
                {
                        head = p1;
                }
                else
                {
                        p2->next = p1;
                }
                p2 = p1;
                p1 = (struct student *)malloc(LEN);
                printf("please input the num:");
                scanf("%d",&p1->num);
                printf("please input the score:");
                scanf("%f",&p1->score);
        }
        p2->next = NULL;
        return head;
}
void print(struct student *head)
{
        struct student *p;
        p = head;
        while(p->num != 0)
        {
                printf("学号为 %d 的成绩为: %f\n",p->num,p->score);
                p = p->next;
        }
}
struct student *del(struct student *head,int num)
{
        struct student *p1,*p2;
        if(head==NULL)
        {
                printf("this is a empty list!\n");
                goto END;
        }
        p1 = head;
        while(p1->num!=num&&p1->next!=NULL)
        {
                p2 = p1;
                p1 = p1->next;
        }
        if(p1->num==num)
        {
                if(p1==head)
                {
                        head = p1->next;
                }
                else
                {
                        p2->next = p1->next;
                }
                printf("Delete No:%d      completed!\n",num);
                n = n - 1;
        }
        else
        {
                printf("No:%d is not found!\n",num);
        }
END:
        return head;
}

isolation 发表于 2018-11-15 12:19:34

很急啊,来个大佬帮帮忙

emmmmmmmmmmm 发表于 2018-11-15 12:58:21

        while (p->next != NULL)
        {
                printf("学号为 %d 的成绩为: %f\n", p->num, p->score);
                p = p->next;
        }这里改成p->next!=null原文是p->num!=0

isolation 发表于 2018-11-15 15:56:00

emmmmmmmmmmm 发表于 2018-11-15 12:58
while (p->next != NULL)
        {
                printf("学号为 %d 的成绩为: %f\n", p->num, p->score);


厉害呀,谢谢大佬{:5_106:}

isolation 发表于 2018-11-15 16:30:18

emmmmmmmmmmm 发表于 2018-11-15 12:58
while (p->next != NULL)
        {
                printf("学号为 %d 的成绩为: %f\n", p->num, p->score);


大佬能给我解释一下为啥改了就可以了吗,没想明白{:10_254:}

isolation 发表于 2018-11-15 16:50:17

emmmmmmmmmmm 发表于 2018-11-15 12:58
while (p->next != NULL)
        {
                printf("学号为 %d 的成绩为: %f\n", p->num, p->score);


这样会漏掉最后一组数据啊

emmmmmmmmmmm 发表于 2018-11-15 18:02:18

void print(struct student *head)
{
        struct student *p;
        p = head;
        while (p->next != NULL)
        {
                printf("学号为 %d 的成绩为: %f\n", p->num, p->score);
                p = p->next;
        }
        printf("学号为 %d 的成绩为: %f\n", p->num, p->score);
}

emmmmmmmmmmm 发表于 2018-11-15 18:06:05

emmm

emmmmmmmmmmm 发表于 2018-11-15 18:07:46

我也不知道该怎么解释,自己看上面的图吧
页: [1]
查看完整版本: 关于删除链表的程序