如果把栈看成一个标尺,每个元素对应了一个刻度
#include<stdio.h>
#include<stdlib.h>
#define N 10
struct stack {
int top;//记录栈中有多少个元素,data[top]表示栈顶
int data[N];
};
struct stack mystack= {
-1,{0}
};//-1代表栈中没有元素,{0}将数组全部初始化为0
//判断栈是否为空。1代表栈为空,0代表不为空
int isempty() {
if(mystack.top==-1) {
return 1;
} else {
return 0;
}
}
//设置栈为空
void setempty() {
mystack.top=-1;
}
//压入一个数据,成功返回1,失败返回0(栈溢出)
int push(int data) {
if(mystack.top+1<=N-1) {//没有溢出
mystack.data[++mystack.top]=data;
return 1;
} else {
return 0;
}
}
//取出一个数组
int pop() {
if(isempty()) {
return -1;//为空,返回-1
} else {
return mystack.data[mystack.top--];
}
}
//一个测试,将十进制数转化为二进制
void tenToTwo(int n){
if(n==0){
return;
}else{
int m=n%2;
tenToTwo(n/2);
printf("%d ",m);//打印出来的是逆序的
}
}
main(){
// int a[10];
// for(int i=0;i<10;i++){
// a[i]=i+1;
// }
// for(int i=0;i<10;i++){
// push(a[i]);//压人数据
// }
// while(isempty()!=1){
// printf("%d\n",pop());
// }
tenToTwo(100);
}
说白了top标志数组的下标