#include <iostream>
#define maxn 1000
using namespace std;
struct Stack{
int Data[maxn];
int Top;
};
Stack *CreateStack(){
Stack *PtrS = (Stack*)malloc(sizeof(Stack));
PtrS->Top=-1;
return PtrS;
}
void Push(Stack *stack,int num){
if(stack->Top==maxn-1){
cout<<"堆栈已满,无法写入";
}else{
stack->Data[++(stack->Top)]=num;
}
}
int Pop(Stack *stack){ //删除并返回栈顶元素
if(stack->Top==-1){
cout<<"堆栈为空,无法删除";
}else{
// int temp=stack->Data[stack->Top];
// stack->Data[stack->Top]=0;
// stack->Top--;
// return temp;
return (stack->Data[(stack->Top)--]);
}
}
bool isFull(Stack *stack){
if(stack->Top==maxn-1){
return true;
}else{
return false;
}
}
bool isEmpty(Stack *stack){
if(stack->Top==-1){
return true;
}else{
return false;
}
}
void print(Stack *stack){
for(int i=0;i<=stack->Top;++i){
cout<<stack->Data[i]<<' ';
}
cout<<endl;
}
int main()
{
Stack *stack = CreateStack();
Push(stack,1);
print(stack);
Push(stack,2);
print(stack);
cout<<Pop(stack)<<endl;
print(stack);
return 0;
}
顺序结构(数组)的堆栈
最新推荐文章于 2022-11-06 22:19:53 发布