ひなた723 2023-06-06 21:05 采纳率: 0%
浏览 11

关于*&head问题(**head)



void loadfacility(node* head)
{
    FILE* file = fopen(".u.info", "r");
    if (!file) {
        printf("没有学生文件,跳过读取\n");
        return;
    }
    node* fresh = (node*)malloc(sizeof(node));
    fresh->next = NULL;
    fresh->sibling = NULL;
    node* move = head->next;
    node* p = move->next;
    while (fread(&fresh->facility, sizeof(Facility), 1, file) == 1)
    {
        if (head->next == NULL)
        {
            move->next = fresh;
            move = fresh;
            fresh = (node*)malloc(sizeof(node));
            fresh->next = NULL;
            fresh->sibling = NULL; continue;
        }
        if (fresh->facility.type != p->sibling->facility.type)
        {
            p->sibling = fresh;
            p = fresh;

            fresh = (node*)malloc(sizeof(node));
            fresh->next = NULL;
            fresh->sibling = NULL;
        }

        move->next = fresh;
        move = fresh;
        fresh = (node*)malloc(sizeof(node));
        fresh->next = NULL;
        fresh->sibling = NULL;
    }
    free(fresh);
    fclose(file);
    printf("读取成功");
}



void savefacility(node* head)
{
    FILE* file = fopen(".u.info", "w");
    if (file == NULL)
    {
        printf("打开文件失败");
        return;
    }
    node* move = head->next;
    node* p = move->next;
    while (move->sibling != NULL)
    {

        if (fwrite(&move->facility, sizeof(Facility), 1, file) != 1)
        {
            printf("写入失败\n");
            return;
        }
        while (p->next != NULL)
        {
            if (fwrite(&p->facility, sizeof(Facility), 1, file) != 1)
            {
                printf("写入失败\n");
                return;
            }
            p = p->next;
        }
        move = move->sibling;
        p = move->next;
    }

    fclose(file);
}



void insert(node*& head)
{
    node* st[100];
    int front, rear;
    front = rear = -1;
    node* fresh = (node*)malloc(sizeof(node));
    fresh->next = NULL;
    fresh->sibling = NULL;
    printf("请输入设备的编号,名字,类型,库存\n");
    scanf("%d %s %s %d", &fresh->facility.number, fresh->facility.name, fresh->facility.type, &fresh->facility.kc);
    node* move = head;
    node* check = head->next;
    node* p = head->sibling;

    if (check == NULL)
        check = fresh;
    while (p != NULL)
    {
        if (p->facility.type == fresh->facility.type)
        {
            p->next = fresh; break;
        }
        else if (p->sibling == NULL)
            p = fresh;
        p = p->sibling;
    }
    rear = (rear + 1) % 100;
    st[rear] = move;
    while (rear != front)
    {
        front = (front + 1) % 100;
        if (st[front]->next != NULL)
        {
            rear = (rear + 1) % 100;
            st[rear] = st[front]->next;
            if (st[rear]->facility.number == fresh->facility.number)
            {
                printf("编号以重复请重新输入编号:\n");
                scanf("%d", &fresh->facility.type);
            }
            while (st[front]->sibling != NULL)
            {
                rear = (rear + 1) % 100;
                st[rear] = st[front]->sibling;
                if (st[rear]->facility.number == fresh->facility.number)
                {
                    printf("编号以重复请重新输入编号:\n");
                    scanf("%d", &fresh->facility.type);
                }
                p = st[front]->sibling;
            }
        }
    }

    savefacility(head);

    system("cls");
}



为什么这个head->next后面的值为什么没有改变,我在insert函数中改变了head->next的指向,在打印函数中prints中head->next的指向还是空,我void insert(node*& head)没有整体改变head的值嘛
  • 写回答

1条回答 默认 最新

  • MrWang. 2023-06-06 21:47
    关注

    在函数insert中改变了head->next指向后,head指针本身并没有被改变。因此在其他函数中调用head时,其指向始终没有发生改变,仍然指向原来的位置,这是您需要考虑的一点。如果您想要改变head指针本身的位置,建议将参数改为**head,这样可以传递head指针的地址,让函数执行后能够改变head指针指向的位置。
    下面是对insert函数进行修改,使其能够改变head指针本身的位置的示例:

    void insert(node** head) // 将参数改为二级指针
    {
        node* fresh = (node*)malloc(sizeof(node));
        fresh->next = NULL;
        fresh->sibling = NULL;
        printf("请输入设备的编号,名字,类型,库存\n");
        scanf("%d %s %s %d", &fresh->facility.number, fresh->facility.name, fresh->facility.type, &fresh->facility.kc);
        node* move = *head;
        node* check = (*head)->next;
        node* p = (*head)->sibling;
    
        if (check == NULL)
            check = fresh;
        while (p != NULL)
        {
            if (p->facility.type == fresh->facility.type)
            {
                p->next = fresh; break;
            }
            else if (p->sibling == NULL)
                p = fresh;
            p = p->sibling;
        }
    
        // 改变head指针的位置
        if (move == NULL)
            *head = fresh;
        else
        {
            while (move->sibling != NULL)
                move = move->sibling;
            move->sibling = fresh;
        }
    
        savefacility(*head);
    
        system("cls");
    }
    

    在这个示例中,我们将函数参数从node*& head改为node** head,并在函数修改head指针的位置时使用二级指针。同时再函数结尾处也需要相应地改为传递指针的地址。这样可以保证函数执行后能够改变head指针指向的位置。

    评论

报告相同问题?

问题事件

  • 创建了问题 6月6日

悬赏问题

  • ¥30 模拟电路 logisim
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价