STL_Stack.cpp STL_Stack.h 实现了STL库中的Stack /*---------------------------------------------------------------------------------------------------- # #Copyright(c)2022YuankangLiang(XerolySkinner) # #本软件按原样提供,无任何明示或暗示 #在任何情况下,作者都不承担任何损害赔偿责任 # #使用的许可声明: #1.不得歪曲本软件的来源,你不能声称你编写了原始软件. #2.免费授予以任何目的,前提是版权声明出现在所有副本中. #并且版权声明和许可声明同时出现. #3.你有使用,复制,修改,分发,和销售本软件的许可. #4.如果你在产品中使用,产品文档中的声明是赞赏的但不是必须的. #5.本通知不得从任何来源删除或更改. # #YuankangLiang(XerolySkinner) #E-mail:zabbcccbbaz@163.com #QQ:2715099320 #MobilePhone:13005636215 # #Allrightsreserved. */ //---------------------------------------------------------------------------------------------------- //头文件 #include"STL_Stack.h" //---------------------------------------------------------------------------------------------------- //构造函数 STL_rStack::STL_rStack(void){ room=0; size=0; dat=NULL;} //---------------------------------------------------------------------------------------------------- STL_rStack::~STL_rStack(void){ free(dat);} //---------------------------------------------------------------------------------------------------- //成员函数 u8STL_rStack::push(u8var){ //保存分配前的数据头 u8*temp_stacks=dat; //若所需空间小于现有空间 if(size>=room){ //分配新长度 dat=static_cast<u8*>(realloc(dat,++size)); //分配失败,善后并且返回错误 if(dat==NULL){ --size;//恢复原长度 dat=temp_stacks;//恢复数据 returnSTACKS_REAL_ERROR; }//返回错误 room=size; //分配成功,压入数据 dat[size-1]=var; //返回正确指数 returnSTACKS_OK;} //空间剩余,压入数据 dat[(++size)-1]=var; //返回正确指数 returnSTACKS_OK;} //---------------------------------------------------------------------------------------------------- u8STL_rStack::pop(void){ //若数据栈中大于1,缩短成员空间 if(size>1){ --size;//重新分配成员空间 returnSTACKS_OK;} //若数据栈中等于1,重指定头 elseif(size==1){ size=0;//重新分配成员空间 returnSTACKS_OK;} //若数据栈为空,返回空栈错误号 else returnSTACKS_EMPTY;} //---------------------------------------------------------------------------------------------------- u8STL_rStack::top(u8&var){ if(size){ var=dat[size-1]; returnSTACKS_OK;} returnSTACKS_EMPTY;} //---------------------------------------------------------------------------------------------------- u32STL_rStack::The_size(void){ returnsize;} //---------------------------------------------------------------------------------------------------- u32STL_rStack::The_room(void){ returnroom;} //---------------------------------------------------------------------------------------------------- u8STL_rStack::clean(void){ //保存分配前的数据头 u8*temp_stacks=dat; //如果有成员,重新分配空间 if(size){ //重新分配空间 dat=static_cast<u8*>(realloc(dat,size)); //分配失败,善后并返回错误 if(dat==NULL){ --size;//恢复原长度 dat=temp_stacks;//恢复数据 returnSTACKS_REAL_ERROR;}//返回错误 } //无成员栈重新分配成员 else{ free(dat);//释放全部空间 dat=NULL; size=0;}//重指定首部 //无数据返回空栈 room=size;//新的空间大小 returnSTACKS_OK;} //---------------------------------------------------------------------------------------------------- u8STL_rStack::push32(u32var){ push(((u8*)(&var))[3]); push(((u8*)(&var))[2]); push(((u8*)(&var))[1]); push(((u8*)(&var))[0]); returnSTACKS_OK;} //---------------------------------------------------------------------------------------------------- u8STL_rStack::poptop(void){ u8var=0; top(var);pop(); returnvar;} //---------------------------------------------------------------------------------------------------- u32STL_rStack::poptop32(void){ u8dat[4]={0,0,0,0}; top(dat[0]);pop(); top(dat[1]);pop(); top(dat[2]);pop(); top(dat[3]);pop(); return*((u32*)(dat));} //---------------------------------------------------------------------------------------------------- void(*STL_rStack::poptopfun(void))(void){ return(void(*)(void))poptop32();} //---------------------------------------------------------------------------------------------------- voidSTL_rStack::pushfun(void(*fun)(void)){ push32((u32)fun);} //---------------------------------------------------------------------------------------------------- //构造函数 STL_lStack::STL_lStack(void){ mem=0; dat=NULL;} //---------------------------------------------------------------------------------------------------- STL_lStack::~STL_lStack(void){ if(mem)pop();} //---------------------------------------------------------------------------------------------------- //成员函数 u8STL_lStack::push(void*&var,u32size){ _lStack*newdat=(_lStack*)malloc(sizeof(_lStack)); if(newdat==NULL)returnSTACKS_MALL_ERROR; newdat->size=size; newdat->front=dat; newdat->dat=var; dat=newdat; ++mem; returnSTACKS_OK;} //---------------------------------------------------------------------------------------------------- u8STL_lStack::pop(void){ if(mem==0)returnSTACKS_EMPTY; _lStack*savedat=dat;//保存首部 dat=dat->front;//弹出 free(savedat->dat);//回收目标指针 free(savedat);//回收 --mem;//成员自减 returnSTACKS_OK;} //---------------------------------------------------------------------------------------------------- u8STL_lStack::top(void*&var,u32&size){ if(mem==0)returnSTACKS_EMPTY; var=dat->dat; size=dat->size; returnSTACKS_OK;} //---------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------- u8STL_lStack::push8(u8var){ void*num=(u8*)malloc(sizeof(u8)); if(num==NULL)returnSTACKS_MALL_ERROR; *(u8*)num=var; push(num,sizeof(u8)); returnSTACKS_OK;} //---------------------------------------------------------------------------------------------------- u8STL_lStack::top8(u8&var){ void*vars=NULL; u32sizes=0; if(top(vars,sizes)==STACKS_EMPTY) returnSTACKS_EMPTY; var=*((u8*)vars); returnSTACKS_OK;} //---------------------------------------------------------------------------------------------------- u8STL_lStack::poptop8(void){ u8temp=0; top8(temp); pop(); returntemp;} //---------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------- u8STL_lStack::push32(u32var){ void*num=(u32*)malloc(sizeof(u32)); if(num==NULL)returnSTACKS_MALL_ERROR; *(u32*)num=var; push(num,sizeof(u32)); returnSTACKS_OK;} //---------------------------------------------------------------------------------------------------- u8STL_lStack::top32(u32&var){ void*vars=NULL; u32sizes=0; if(top(vars,sizes)==STACKS_EMPTY) returnSTACKS_EMPTY; var=*((u32*)vars); returnSTACKS_OK;} //---------------------------------------------------------------------------------------------------- u32STL_lStack::poptop32(void){ u32temp=0; top32(temp); pop(); returntemp;} //---------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------- u8STL_lStack::pushfun(void(*fun)(void)){ u8res; res=push32((u32)fun); returnres;} //---------------------------------------------------------------------------------------------------- void(*STL_lStack::poptopfun(void))(void){ return(void(*)(void))poptop32();}