dyj6pdehen 2022-11-23 21:36 采纳率: 78.9%
浏览 2
已结题

函数调用后结果没有改变


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define QueueSize 100//(100可变)
typedef int DataType;
typedef struct Node
{
    DataType data;
    struct Node* next;
}Node;
Node* top1;
int initLinkStack(Node* L)
{
    if (!L->data) return 0;//申请失败
    L->data = 0;//初始化链栈头结点数据域
    L->next = NULL;//初始化链栈头结点指针域
    return 1;
}
void Push(Node* top, DataType x)
{
    Node* s = (Node*)malloc(sizeof(Node));
    s->data = x;
    s->next = top;
    top = s;
}
int Pop(Node* top, DataType* ptr)
{
    if (top == NULL)
    {
        printf("下溢错误,出栈失败\n");
        return 0;
    }
    Node* p = top;
    *ptr = p->data;
    top = p->next;
    free(p);
    return 1;
}
void PrintL(Node* L,Node *top)
{
    Node* p = top;
    if (p->next== NULL) printf("已空,打印失败\n");
    else
    {
        while (p != L)
        {
            printf("%d ",p->data);
            p = p->next;
        }
        printf("\n");
    }
int main()
{
    Node* L=(Node*)malloc(sizeof(Node));
    initLinkStack(L);
    top1 = L;
    Push(top1, 1);//之后top1值为0
    PrintL(L,top1);
}

能运行,但是打印时top1的data仍然为0,在函数里面的时候top的data是1没有问题的,很奇怪,来人看看

img

  • 写回答

1条回答 默认 最新

  • 快乐鹦鹉 2022-11-23 22:14
    关注

    push函数不行,函数内是不能实现s指针地址的修改的。所以外部的top1指针并没有被改变

     
    #include
    #include
    #include
    #define QueueSize 100//(100可变)
    typedef int DataType;
    typedef struct Node
    {
        DataType data;
        struct Node* next;
    }Node;
    Node* top1;
    int initLinkStack(Node* L)
    {
        if (!L->data) return 0;//申请失败
        L->data = 0;//初始化链栈头结点数据域
        L->next = NULL;//初始化链栈头结点指针域
        return 1;
    }
    void Push(Node** top, DataType x)
    {
        Node* s = (Node*)malloc(sizeof(Node));
        s->data = x;
        s->next = *top;
        *top = s;
    }
    int Pop(Node** top, DataType* ptr)
    {
        if (*top == NULL)
        {
            printf("下溢错误,出栈失败\n");
            return 0;
        }
        Node* p = *top;
        *ptr = p->data;
        *top = p->next;
        free(p);
        return 1;
    }
    void PrintL(Node* L,Node *top)
    {
        Node* p = top;
        if (p->next== NULL) printf("已空,打印失败\n");
        else
        {
            while (p != L)
            {
                printf("%d ",p->data);
                p = p->next;
            }
            printf("\n");
        }
    int main()
    {
        Node* L=(Node*)malloc(sizeof(Node));
        initLinkStack(L);
        top1 = L;
        Push(&top1, 1);//之后top1值为0
        PrintL(L,top1);
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 12月1日
  • 已采纳回答 11月23日
  • 创建了问题 11月23日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么