有望. 2022-03-08 15:45 采纳率: 100%
浏览 63
已结题

vs2022运行链栈出现问题

想请教一下,我在此感激不尽!
#include <stdio.h>

#include <stdlib.h>
#include <malloc.h>

#define stacksize 100
typedef int Datetype;

//节点
typedef struct Node
{
Datetype date;// [stacksize] ;
struct stack *next;
}Node, *pNode;

//栈顶栈底
typedef struct stack
{
pNode base;
pNode top;
}stack,*pstack;

//初始化
void init(pstack s)
{
s->base = (Node*)malloc(sizeof(Node));
if (s->base == NULL)
{
printf("失败.\n");
exit(-1);
};
s->base->next = NULL;
s->top = s->base;
}

//进栈
void insert(pstack s, Datetype x)
{
pNode p = (Node*)malloc(sizeof(Node));
if (p == NULL)
{
printf("失败.\n");
exit(-1);
};
p->date = x;
p->next = s->top;
s->top = p;
}

//输出栈的数据
void Printf(pstack s)
{
pNode p = s->top;
printf("栈数据为:");
while (p != s->base)
{
printf(" %d", p->date);
p = p->next;
}
printf("\n");
}

//判空操作
int empty(pstack s)
{
if (s->top == s->base)
{
return 1;
}
else
{
return 0;
}
}

//销毁栈
int destroy(pstack s)
{
//int n = empty(s);
if (empty(s))
{
return 1;
};
pNode p = NULL;
while (s->top != s->base)
{
p = s->top;
s->top = s->top->next;
free(p);
p = NULL;
}
}

//主函数
void main(void)
{
pstack s = NULL;
int x = 0;
printf("请输入数据:");
scanf_s("%d", &x);
init(s);
insert(s, x);
insert(s, 2);
Printf(s);
destroy(s);
}

img

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • Code_流苏 C/C++领域优质创作者 2022-03-08 15:58
    关注

    img


    nullptr是空指针的意思
    所以异常就在于s是空指针

    希望对题主有所帮助,可以的话,帮忙点个采纳!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月25日
  • 已采纳回答 4月17日
  • 创建了问题 3月8日

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?