这个简单的出栈入栈为哪里错了啊,求大佬
为什么这个简单的栈的代码一直显示这个错误,实在找不出来哪里错了,如图:#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct Node{
int val;
struct Node* next;
}NODE,*PNODE;
typedef struct Stack{
struct Node* sTop;
struct Node* sBottom;
}STACK,*PSTACK;
//栈的初始化
void init(PATACK s){
s->sBottom=(PNODE)malloc(sizeof(NODE));
s->sTop=s->sBottom;
s->sBottom->next=NULL;
}
//进栈
void push(PSTACK s,int val){
PNODE p=(PNODE)malloc(sizeof(NODE));
p->val=val;
p->next=s->sTop;
s->sTop=p;
}
//出栈
void pop(PSTACK s,int val){
PNODE p;
//确保栈为非空时,再出栈
if(empty(s)){
printf("出栈失败!因为栈为空\n");
return false;
}
//删除当前栈的结点
p=s->sTop;
s->sTop=s->sTop->next;
free(p);
//输出需要出栈的元素
printf("出栈元素为:%d\n",val);
}
//栈是否为空
bool empty(PSTACK s){
if(s->sBottom==s->sTop)
return ture;
else
return false;
}
main(){
//定义一个栈
Stack s;
//初始化栈
init(&s)
//进栈
push(&s,5);
push(&s,6);
push(&s,7);
push(&s,8);
push(&s,9);
//遍历
//出栈
pop(&s,9);
pop(&s,8);
pop(&s,7);
pop(&s,6);
} 不是PSTACK吗?咋变成PATACK了 qiuyouzhi 发表于 2020-7-16 10:40
不是PSTACK吗?咋变成PATACK了
我看看哈,难道我写错了 爱学习520 发表于 2020-7-16 10:41
我看看哈,难道我写错了
还有一堆错误,都帮你改过来了:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
# include <stdbool.h>
typedef struct Node{
int val;
struct Node* next;
}NODE,*PNODE;
typedef struct Stack{
struct Node* sTop;
struct Node* sBottom;
}STACK,*PSTACK;
//栈是否为空
bool empty(PSTACK s){
if(s->sBottom==s->sTop)
return true;
else
return false;
}
//栈的初始化
void init(PSTACK s){
s->sBottom=(PNODE)malloc(sizeof(NODE));
s->sTop=s->sBottom;
s->sBottom->next=NULL;
}
//进栈
void push(PSTACK s,int val){
PNODE p=(PNODE)malloc(sizeof(NODE));
p->val=val;
p->next=s->sTop;
s->sTop=p;
}
//出栈
bool pop(PSTACK s,int val){
PNODE p;
//确保栈为非空时,再出栈
if(empty(s)){
printf("出栈失败!因为栈为空\n");
return false;
}
//删除当前栈的结点
p=s->sTop;
s->sTop=s->sTop->next;
free(p);
//输出需要出栈的元素
printf("出栈元素为:%d\n",val);
}
int main(){
//定义一个栈
STACK s;
//初始化栈
init(&s);
//进栈
push(&s,5);
push(&s,6);
push(&s,7);
push(&s,8);
push(&s,9);
//遍历
//出栈
pop(&s,9);
pop(&s,8);
pop(&s,7);
pop(&s,6);
} qiuyouzhi 发表于 2020-7-16 10:45
还有一堆错误,都帮你改过来了:
谢谢大佬,我已经都运行通过啦,刚刚是写错了,哈哈{:5_109:}
页:
[1]