C语言链栈,栈顶元素返回问题

2021-02-23 18:44发布

求求各位大佬了,为什么a返回的是地址啊!

#include<stdio.h> #include<stdlib.h> typedef int Status; typedef int ElemType; #define TRUE 1; #define ERROR 0; typedef struct Node { ElemType data; struct Node* next; }StackNode,*StackList; Status InitStack(StackList S); //初始化(无头结点) Status Push(StackList S, ElemType e); //头插法 Status Pop(StackList S, ElemType* a); //弹出 int main() { int n; ElemType e, a; StackList S; S = (StackList)malloc(sizeof(StackNode)); if (InitStack(S) == 1) { printf("恭喜!构建空链栈成功!\n"); } printf("请输入插入元素个数:"); scanf("%d", &n); printf("请输入插入栈的元素:"); for (int i = 0; i < n; i++) { scanf("%d", &e); Push(S, e); } printf("恭喜你!插入成功!\n"); if (Pop(S, &a) == 1) { printf("恭喜你!出栈成功!\n"); printf("删除栈顶元素:%d\n", a); } return 0; } Status InitStack(StackList S) { S = NULL; return TRUE; } Status Push(StackList S, ElemType e) { StackList P; P = (StackList)malloc(sizeof(StackNode)); P->data = e; P->next = S->next; S = P; //修改栈定指针 return TRUE; } Status Pop(StackList S, ElemType *a) { StackList P; P = S; if (P == NULL) return ERROR; *a = S->data; S = S->next; free(P); return TRUE; }

 

1条回答
Nefu_lyh
1楼-- · 2021-02-23 19:00

PUSH阶段中a的值出错了,你的S = P那里不知道为什么没有成功,S = P之后,S->data的值很奇怪,我再看看