犯错=成长
编写顺序栈时出现下边的提醒
31[Error] base operand of '->' has non-pointer type 'stack'
直接上代码(错误版本):
#include <stdlib.h>
#include <stdio.h>
#define SIZE 1000
typedef struct{
int data[SIZE];
int top;
}stack;
void Init(stack &s){//初始化棧,棧里面放的元素不一定是什么
s.top=-1;
} //初始化完成,各种参数也已经确定
void Push(stack &s,int e){
if(s.top==SIZE-1)printf("已经满了~");
else
s.data[++(s.top)]=e;
}
void Pop(stack &s,int &e){
if(s.top==-1)printf("没有元素,无法抛出!");
e=s.data[(s.top)--];
}
void Print(stack &s){
while(s.top!=-1)
printf("%d",s.data[s.top--]);
}
int main(){
stack s;
Init(s);
Push(s,2);
Push(s,2);
Push(s,2);
Print(s);
return 0;
}
=============================================================
以下是正确版本:
#include <stdlib.h>
#include <stdio.h>
#define SIZE 1000
typedef struct{
int data[SIZE];
int top;
}stack;
void Init(stack &s){//初始化棧,棧里面放的元素不一定是什么
s.top=-1;
} //初始化完成,各种参数也已经确定
void Push(stack &s,int e){
if(s.top==SIZE-1)printf("已经满了~");
else
s.data[++(s.top)]=e;
}
void Pop(stack &s,int &e){
if(s.top==-1)printf("没有元素,无法抛出!");
e=s.data[(s.top)--];
}
void Print(stack &s){
while(s.top!= -1)
printf("%d",s.data[s.top--]);
}
int main(){
stack s;
Init(s);
Push(s,2);
Push(s,2);
Push(s,2);
Print(s);
return 0;
}
反思:之前从来没有太注意 “.”以及”->”之间的区别,今天才发现有多么严重。”.”适用于结构体变量,”->”适用于结构体指针变量!!!!
还有,一定要记得初始化~## 标题 ##