函数调用后结果没有改变


#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);
}

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

img

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);
}