您的位置:首页 > 其它

顺序栈基本运算的实现

2017-09-26 10:53 253 查看
#include<iostream>
using namespace std;

#define ListSize 11
typedef int DataType;
struct Stack{
DataType data[ListSize];
int top; //除了记录大小,还可以记录栈顶位置
};

void InitStack(struct Stack *stack){
stack->top = 0;
}

bool StackEmpty(struct Stack *stack){
return stack->top == 0;
}

void push(struct Stack *stack, DataType d){
if(stack->top == ListSize){
return ;
}
stack->data[stack->top++] = d;
}

void pop(struct Stack *stack){
if(StackEmpty(stack))
return;
stack->top--;
}

DataType topData(struct Stack *stack){
return stack->data[stack->top-1];
}

int main(){

struct Stack stack;
InitStack(&stack);
push(&stack,1);
push(&stack,5);
push(&stack,8);
push(&stack,2);
push(&stack,1);
push(&stack,0);
push(&stack,6);
push(&stack,3);
push(&stack,4);
push(&stack,6);
push(&stack,4);

while(!StackEmpty(&stack)){
cout<<topData(&stack)<<endl;
pop(&stack);
}

cout<<endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: