鱼C论坛

 找回密码
 立即注册
查看: 2238|回复: 2

关于删除动态链表的问题,在VS2018上面运行的

[复制链接]
发表于 2019-7-3 12:37:02 | 显示全部楼层 |阅读模式

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

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

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

struct student
{
        int num;
        float score;
        struct student* next;
};
#define LEN sizeof(struct student)
struct student* creat();
struct student* delete(struct student *head,int num);
void print();
int n;

void main()
{
        struct student* stu, * p;
        int n;

        stu = creat();
        p = stu;
        print(p);
#if 1
        printf("Please enter the num to delete:");
        scanf_s("%d", &n);
        print(delete(p, n));
#endif
        printf("\n\n");
        system("pause");
}
struct student* creat()
{
        struct student* head;
        struct student* p1, * p2;

        p1 = p2 = (struct student*)malloc(LEN);
        printf("Please enter the num :");
        scanf_s("%d", &p1->num);

        printf("Please enter the score :");
        scanf_s("%f", &p1->score);

        n = 0;
        head = NULL;

        while (p1->num)
        {
                n++;
                if (n == 1)
                {
                        head = p1;
                }
                else
                {
                        p2->next = p1;
                }
                p2 = p1;

                p1 = (struct student*)malloc(LEN);

                printf("Please enter the num :");
                scanf_s("%d", &p1->num);

                if (p1->num != 0)
                {
                        printf("Please enter the score :");
                        scanf_s("%f", &p1->score);
                }
        }
        p2->next = NULL;
       
        return head;

}
#if 1
struct student* delete(struct student* head, int num)
{
        struct student* q1, * q2;
        if (head == NULL)
        {
                printf("\nThis list is NULL!\n");
                goto END;
        }
        q1 = head;
        while (q1->num != 0 && q1->next != NULL)
        {
                q2 = q1;
                q1 = q1->next;
        }
        if (q1->num == num)
        {
                if (q1 == head)
                {
                        head = q1->next;
                }
                else
                {
                        q2->next = q1->next;
                }
                printf("\nDelete NO:%d succeed!\n", num);
                n = n - 1;
        }
        else
        {
                printf("%d not been found!\n",num);
        }
END:
        return head;
}
#endif

void print(struct student* head)
{
        struct student* p;
        printf("There are %d record\n", n);
        p = head;
        if (head)
        {
                do
                {
                        printf("学号%.2d学生成绩为%2.1f\n", p->num, p->score);
                        p = p->next;
                } while (p);
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-7-3 12:40:29 | 显示全部楼层
会报“CL.exe"已退出,代码为2
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-3 13:36:58 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 23:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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