#include<stdio.h>
typedef int ElemType;
typedef struct Node {
ElemType data;
struct Node* next;
}Node;
//初始化
void InitNode(Node *D){
D = NULL;
printf("%d\n",D);
}
//判空
int NodeEmpty(Node* D) {
if (D == NULL) {
printf("栈为空\n");
return 0;
}
else {
printf("栈不为空\n");
return 1;
}
}
//插入元素
int InNode(Node* D, int x) {
Node* New1 = (Node*)malloc(sizeof(Node));
if (New1 == NULL) {
return 0;
}
New1->data = x;
New1->next = D;
D = New1;
printf("执行第之后,插入操作后D的值为:%d\n", D->data);
return 1;
}
//出栈
int OutNode(Node* D) {
if (D != NULL) {
int x;//用于记录出栈值
x = D->data;
//用malloc建立的一定要free
Node* De = (Node*)malloc(sizeof(Node));
De = D;
D = De->next;
free(De);
printf("出栈之后D.next=%d\n", D->data);
return x;
}
else {
printf("栈为空\n");
return 0;
}
}
//输出栈顶元素
int OutTopNode(Node* D) {
if (D != NULL) {
printf("栈顶模块内栈顶元素为:%d\n",D->data);
return D->data;
}
else {
printf("栈为空\n");
return 0;
}
}
void main() {
Node D;
InitNode(&D);
InNode(&D, 1);
NodeEmpty(&D);
int OutPut1 = OutTopNode(&D);
printf("第1次栈顶元素为%d\n",OutPut1);
//int OUT1 = OutNode(&D);
//printf("第1次出栈元素:%d\n", OutPut1);
//int OutPut2 = OutTopNode(&D);
//printf("第2次栈顶元素为%d\n", OutPut2);
//NodeEmpty(&D);
}
显示指针指向负数
点个采纳吧!
所以你要改成指针,然后函数参数改用二级指针,就可以了。
#include<stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct Node {
ElemType data;
struct Node* next;
}Node;
//初始化
void InitNode(Node **D) {
(*D) = (Node*)malloc(sizeof(Node));
(*D)->data = -1;
(*D)->next = NULL;
printf("%d\n", *D);
}
//判空
int NodeEmpty(Node **D) {
if ((*D) == NULL) {
printf("栈为空\n");
return 0;
} else {
printf("栈不为空\n");
return 1;
}
}
//插入元素
//int InNode(Node* D, int x) {
// Node* New1 = (Node*)malloc(sizeof(Node));
// if (New1 == NULL) {
// return 0;
// }
// New1->data = x;
// New1->next = D;
// D = New1;
// printf("执行第之后,插入操作后D的值为:%d\n", D->data);
// return 1;
//}
int InNode(Node **D, int x) {
Node* New1 = (Node*)malloc(sizeof(Node));
New1->data = x;
New1->next = (*D);
*D = New1;
printf("执行第之后,插入操作后D的值为:%d\n", (*D)->data);
return 1;
}
//出栈
int OutNode(Node* D) {
if (D != NULL) {
int x;//用于记录出栈值
x = D->data;
//用malloc建立的一定要free
Node* De = (Node*)malloc(sizeof(Node));
De = D;
D = De->next;
free(De);
printf("出栈之后D.next=%d\n", D->data);
return x;
} else {
printf("栈为空\n");
return 0;
}
}
//输出栈顶元素
int OutTopNode(Node **D) {
if ((*D) != NULL) {
printf("栈顶模块内栈顶元素为:%d\n", (*D)->data);
return (*D)->data;
} else {
printf("栈为空\n");
return 0;
}
}
void main() {
Node *D;
InitNode(&D);
InNode(&D, 1);
NodeEmpty(&D);
int OutPut1 = OutTopNode(&D);
printf("第1次栈顶元素为%d\n", OutPut1);
//int OUT1 = OutNode(&D);
//printf("第1次出栈元素:%d\n", OutPut1);
//int OutPut2 = OutTopNode(&D);
//printf("第2次栈顶元素为%d\n", OutPut2);
//NodeEmpty(&D);
}
写一些打印语句调试一下,关键的变量值打印出来看看。
其实问题就出来这个插入这里,这里并没进行插入,当函数执行退出后,D还是原来那个D并没有数据插入。你可以打个断点看看。
//插入元素
int InNode(Node* D, int x) {
Node* New1 = (Node*)malloc(sizeof(Node));
if (New1 == NULL) {
return 0;
}
New1->data = x;
New1->next = D;
D = New1;
printf("执行第之后,插入操作后D的值为:%d\n", D->data);
return 1;
}
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y
C和C++完整教程:https://blog.csdn.net/it_xiangqiang/category_10581430.html
C和C++算法完整教程:https://blog.csdn.net/it_xiangqiang/category_10768339.html