鱼C论坛

 找回密码
 立即注册
查看: 1945|回复: 8

关于删除链表的程序

[复制链接]
发表于 2018-11-15 12:06:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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






#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;
}
QQ截图20181115120521.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-11-15 12:19:34 | 显示全部楼层
很急啊,来个大佬帮帮忙
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-15 15:56:00 | 显示全部楼层
emmmmmmmmmmm 发表于 2018-11-15 12:58
while (p->next != NULL)
        {
                printf("学号为 %d 的成绩为: %f\n", p->num, p->score);

厉害呀,谢谢大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-15 16:30:18 | 显示全部楼层
emmmmmmmmmmm 发表于 2018-11-15 12:58
while (p->next != NULL)
        {
                printf("学号为 %d 的成绩为: %f\n", p->num, p->score);

大佬能给我解释一下为啥改了就可以了吗,没想明白
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-15 16:50:17 | 显示全部楼层
emmmmmmmmmmm 发表于 2018-11-15 12:58
while (p->next != NULL)
        {
                printf("学号为 %d 的成绩为: %f\n", p->num, p->score);

这样会漏掉最后一组数据啊
QQ截图20181115164922.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-15 18:06:05 | 显示全部楼层
emmm
1.PNG
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-11-15 18:07:46 | 显示全部楼层
我也不知道该怎么解释,自己看上面的图吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-2 22:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表