數據結構-順序棧(進棧 出棧)


#include<stdio.h>
#define MaxSize 10

typedef struct SqStack{
    int data[MaxSize];
    int top ;
}SqStack;

//初始化順序棧
void initStack(SqStack &S){
    S.top = -1; 
} 

//判斷棧是否為空   /*棧理論上不存在為滿的情況,取決於內存大小*/
int isEmpty(SqStack S){
    if(S.top == -1){//top為1表示為空 
        return 1;
    }else{
        return 0;
    } 
} 

//進棧操作
int push(SqStack &S,int e){
    //進棧要判滿
    if(S.top == MaxSize-1){
        return 0;
    } 
    //先移動指針在進行復制 
    ++(S.top);      
    S.data[S.top] = e;
    return 1;
}

//出棧操作
int pop(SqStack &S,int &e){
    //出棧要判空
    if(S.top == -1){
        return 0;
    } 
    //先進行賦值在移動指針 
    e = S.data[S.top];
    --(S.top);
    return 1;
}

//展示棧中的元素
void show(SqStack &S){
    int e;
    while(S.top>-1){
        pop(S,e);
        printf("%d    ",e);
    }
} 
//持續往棧中存放元素
void put(SqStack &S){
    initStack(S);  ///一定要初始化 
    int e;
    scanf("%d",&e);
    while(S.top<MaxSize&&e!=-1){
        push(S,e);
        printf("%d    ===\n ",S.top);
//        printf("輸入成功\n");
        scanf("%d",&e);
    }
} 
int main(){
    SqStack S;
    put(S);
    show(S);
    return 0;
} 
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM