Ggzy_ 2021-10-27 07:50 采纳率: 66.7%
浏览 41
已结题

这段代码为什么一直循环啊


//C语言构建链表
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stud_node {
    int num;
    char name[20];
    int score;
    struct stud_node* next;
};
struct stud_node* Create_Stu_Doc();//新建链表
struct stud_node* InsrtDoc(struct stud_node* head, struct stud_node* stud);//插入
struct stud_node* DeleteDoc(struct stud_node* head, int num);//删除
void Print_stu_Doc(struct stud_node* head);//遍历
int main()
{
    struct stud_node* head, * p;
    int choice, num, score;
    char name[20] = {0};
    head = NULL;
    int size = sizeof(struct stud_node);

    do {
        printf("1:create 2:insert 3:delete 4:print 0:exit\n");
        scanf_s("%d", &choice);
        switch (choice) {
        case 1:
            head= Create_Stu_Doc();
            break;
        case 2:
            printf("Input num,name and score:\n");
            scanf_s("%d%s%d",&num,name,20,&score);
            p = (struct stud_node*)malloc(size);
            if (p == NULL)
                printf("内存分配不成功\n");
            else
            {
                p->num = num;
                strcpy_s(p->name, 20, name);
                p->score = score;
                head = InsrtDoc(head, p);
            }
            break;
        case 3:
            printf("Input num:\n");
            scanf_s("%d", &num);
            head = DeleteDoc(head, num);
            break;
        case 4:
            Print_stu_Doc(head);
            break;
        case 0:
            break;
        }
    } while (choice != 0);

    return 0;
}
//新建链表
struct stud_node* Create_Stu_Doc()
{
    struct stud_node* head, * p;
    int num, score;
    char name[20] = {0};
    int size = sizeof(struct stud_node);

    head = NULL;
    printf("Input num,name and score:\n");
    scanf_s("%d%s%d",&num,name,20,&score);
    while (num != 0)
    {
        p = (struct stud_node*)malloc(size);
        if (p == NULL)
            printf("内存分配不成功\n");
        else
        {
            p->num = num;
            strcpy_s(p->name, 20, name);
            p->score = score;
        }
        head = InsrtDoc(head, p);     //调用插入函数
        scanf_s("%d%s%d", &num, name,20, &score);
    }
    return head;
}
//插入
struct stud_node* InsrtDoc(struct stud_node* head, struct stud_node* stud)
{
    struct stud_node* ptr, * ptr1, * ptr2;
    ptr2 = head;
    ptr = stud;
    if (head == NULL)
    {
        head = ptr;
        head->next = NULL;
    }
    else{
        while ((ptr->num > ptr2->num) && (ptr2->next != NULL)) {
            ptr1 = ptr2;
            ptr2 = ptr2->next;
        }
        if (ptr->num <= ptr2->num) {
            if (head == ptr2) head = ptr;
            else ptr1->next = ptr;
            ptr->next = ptr2;
        }
        else {
            ptr2->next = ptr;
            ptr->next = NULL;
        }
    }
    return head;
}
//删除
struct stud_node* DeleteDoc(struct stud_node* head, int num)
{
    struct stud_node * ptr1, * ptr2;
    while (head != NULL && head->num == num) {
        ptr2 = head;
        head = head->next;
        free(ptr2);
    }
    if (head == NULL)
        return NULL;
    ptr1 = head;
    ptr2 = head->next;
    while (ptr2 != NULL) {
        if (ptr2->num == num) {
            ptr1->next = ptr2->next;
            free(ptr2);
        }
        else
        ptr1 = ptr2;
        ptr2 = ptr1->next;
    }
    return head;
}
//遍历
void Print_stu_Doc(struct stud_node* head)
{
    struct stud_node*ptr;
    if (head == NULL)
    {
        printf("\nNo Records\n");
        return;
    }
    printf("\nThe Students'Records Are:\n");
    printf("num\tname\tscore\n");
    for(ptr=head;ptr!=NULL;ptr=ptr->next)
        printf(" %d\t%s\t%d\n",ptr->num, ptr->name, ptr->score);
}
  • 写回答

1条回答 默认 最新

  • J_KILL_P 2021-10-27 08:37
    关注

    用了do……while循环,每一次循环输入choice值进行判断是选择功能还是退出循环,想要退出循环需要根据while后面括号的值,题主while(choice!=0),也就是说只要choice不等于0就会一直循环,想要退出循环输入0即可。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月4日
  • 已采纳回答 10月27日
  • 创建了问题 10月27日

悬赏问题

  • ¥15 数据库获取信息反馈出错,直接查询了ref字段并且还使用了User文档的_id而不是自己的
  • ¥15 将安全信息用到以下对象时发生以下错误:c:dumpstack.log.tmp 另一个程序正在使用此文件,因此无法访问
  • ¥15 速度位置规划实现精确定位的问题
  • ¥15 代码问题:df = pd.read_excel('c:\User\18343\Desktop\wpsdata.xlxs')路径读不到
  • ¥15 为什么视频算法现在全是动作识别?
  • ¥15 编写一段matlab代码
  • ¥15 用Python做岩石类别鉴定软件
  • ¥15 关于调取、提交更新数据库记录的问题
  • ¥15 之前删了盘从下vs2022遇见这个问题 搞了一整天了
  • ¥15 从Freecad中宏下载的DesignSPHysics,出现如下问题是什么原因导致的(语言-python)