#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<malloc.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 10
#define INIT_SIZE 10//线性表初始长度
#define INCREMENT_SIZE 10//增量
using namespace std;
typedef int ElemType;
typedef int Status;
typedef struct{
ElemType* base;
ElemType* top;
int stackSize;
}Stack;
Status initStack(Stack& s){
s.top=s.base=(ElemType*)malloc(sizeof(ElemType)*INIT_SIZE);
if(!s.top)exit(OVERFLOW);
s.stackSize=INIT_SIZE;
return OK;
}
Status push(Stack& s,ElemType e){
if(s.top-s.base>=s.stackSize-1){
s.base=(ElemType*)realloc(s.base,sizeof(ElemType)*(s.stackSize+INCREMENT_SIZE));
if(!s.base)exit(OVERFLOW);
s.stackSize=s.stackSize+INCREMENT_SIZE;
}
*(s.top)=e;
s.top++;
return OK;
}
Status getTop(Stack&s ,ElemType &e){
if(s.base==s.top)return ERROR;
e=*(s.top-1);
return OK;
}
Status pop(Stack& s,ElemType& e){
if(s.base==s.top)return ERROR;
e=*(s.top-1);
s.top--;
return OK;
}
Status isEmpty(Stack& s){
if(s.base==s.top)return OK;
return ERROR;
}
int main(){
Stack s;
initStack(s);
for(int i=0;i<15;i++){
push(s,i);
}
ElemType t;
while(!isEmpty(s)){
pop(s,t);
printf("%d ",t);
}
return 0;
}
stack
最新推荐文章于 2022-03-27 16:11:51 发布