#include <iostream>
using namespace std;
const int initStackSize = 10;
const int stackIncrement=10;
static int values=1;
typedef struct stack{
int *top;
int *base;
int stackSize;
}stack;
void initStack(stack &s){
s.base = (int *)malloc(initStackSize*sizeof(int));
if(!s.base)
cout<<"error"<<endl;
s.top = s.base;
s.stackSize = initStackSize;
}
void pushStack(stack &s,int value){
if(s.top - s.base >=s.stackSize){
s.base = (int *)realloc(s.base,sizeof(int)*(initStackSize+stackIncrement));
s.top = s.base+s.stackSize;
s.stackSize += stackIncrement;
}
*s.top++ = value;
}
int popStack(stack &s){
if(s.top>s.base)
return *--s.top;
else
return 0;
}
void destroyStack(stack &s){
if(s.base!=NULL)
free(s.base);
s.stackSize = 0;
s.base =s.top = NULL;
}
void main(){
stack s;
initStack(s);
for(int i=0;i<20;i++){
pushStack(s,i+1);
}
for(i=0;i<20;i++){
cout<<popStack(s)<<" ";
}
//cout<<endl;
destroyStack(s);
}
数据结构之栈
最新推荐文章于 2024-07-13 16:00:03 发布