栈
特点
后进先出 先进后出
举例
子弹匣
应用
1程序方法的调用和返回就是将程序入口地址进行入栈和出栈操作
2带括号与正负符号等多重优先级的计算器程序实现
typedef double EleType;
typedef struct{
EleType *buf;
int top;
int max;
}List;
List *CreateList(int max);
int PushStack(List *lp,EleType data);
EleType PopStack(List *lp);
int TraverseList(List *lp,int (*f)(EleType *data));
EleType GetElement(List *lp);
int IsEmpty(List *lp);
int IsFull(List *lp);
void DestroyList(List *lp);
List *CreateList(int max){
List *lp;
lp = (List *)malloc(sizeof(List));
if(!lp){
return 0;
}
lp->buf =(EleType *)malloc(sizeof(EleType)*max);
lp->max = max;
lp->top = -1;
return lp;
}
int PushStack(List *lp,EleType data){
if(IsFull(lp))
return 0;
lp->top++;
lp->buf[lp->top] = data;
return 1;
}
EleType PopStack(List *lp){
EleType a;
if(IsEmpty(lp))
return 0;
a = lp->buf[lp->top];
lp->top--;
return a;
}
int TraverseList(List *lp,int (*f)(EleType *data)){
int a;
if(lp->top==-1)
return 0;
for(a = 0;a<lp->top;a++){
if(IsFull(lp))
return 0;
if(!f(&(lp->buf[a]))){
return a+1;
}
}
return 1;
}
EleType GetElement(List *lp){
EleType a;
if(IsEmpty(lp))
return 0;
a = lp->buf[lp->top];
lp->top--;
return a;
}
int IsEmpty(List *lp){
if(lp->top==-1)
return 1;
else
return 0;
}
int IsFull(List *lp){
if(lp->top==lp->max-1)
return 1;
else
return 0;
}
void DestroyList(List *lp){
free(lp);
}