数据结构复习 -- 顺序栈


#include 
#include 
#include  
#define MaxSize 100
using namespace std ; 
typedef char ElemType ; 
typedef struct {
    ElemType data[MaxSize] ; 
    int top ; 
}SqStack ; 
// 顺序栈

void InitStack(SqStack *&s) {
    s = (SqStack*)malloc(sizeof(SqStack)) ; 
    s->top = -1 ; 
}
void DestoryStack(SqStack *&s){
    free(s) ; 
}
bool StackEmpty(SqStack *s ){
    return s->top == -1 ; 
}
// 进栈
bool Push(SqStack *&s ,ElemType e) {
    if(s->top == MaxSize -1) {
        return false ; // 栈满了
    }
    s->top++; 
    s->data[s->top] = e ; 
    return true ; 
}
// 出栈
bool Pop(SqStack *&s , ElemType &e){

    if(s->top == -1) {
        return false ; 
    }
    e = s->data[s->top] ; 
    s->top -- ; 
    return true ; 
}
bool GetTop(SqStack *s ,ElemType &e) {
    if(s->top == -1) {
       return false ; 
    }
    e = s->data[s->top] ; 
    return true  ;
}
int main() {


    SqStack *S ; 
    InitStack(S); 
    int n ; 
    cin >> n ; 
    for(int i = 0 ; i>x ; 
        Push(S,x) ;
    }
    while(!StackEmpty(S)) {
        ElemType t ;
        GetTop(S,t) ;
        cout<

 

你可能感兴趣的:(数据结构复习 -- 顺序栈)