[C++]数据结构:栈之顺序栈

1 顺序栈的基本概念

2 编程复现

2.1 定义基本数据结构

typedef char DataType; // 基本数据类型 

enum Status { ERROR, OK, OVERFLOW };// 状态(码) : 枚举类型 

#define MAXSIZE_STACK 100 //栈内最大元素数 

typedef struct { // 顺序栈
    DataType *base; // 栈底指针 (NULL:栈结构不存在; 初始化后,始终指向栈底)
    DataType *top;  //栈顶指针 (初始时:top==base; top所指处无任何元素) 
    int stackSize;  //栈可用的最大容量 
}SeqStack; 

2.2 基本操作

Status InitStack(SeqStack &S); //初始化
Status Push(SeqStack &S, DataType e); //入栈
Status Pop(SeqStack &S, DataType &e); //出栈
bool StackEmpty(SeqStack S); //判定栈空
bool StackFull(SeqStack S); //判定栈满
Status GetTop(SeqStack S, DataType &e); //取得栈顶元素
int StackLength(SeqStack &S); //栈的长度
Status StackTraverse(SeqStack S); //栈的遍历
  • 0> 初始化
Status InitStack(SeqStack &S){
    S.base = new DataType [MAXSIZE_STACK];
    if(S.base==NULL){
        exit(OVERFLOW);
    }
    S.top = S.base;
    S.stackSize = MAXSIZE_STACK; // 设置栈的可用最大容量
    return OK; 
}
  • 1> 入栈
Status Push(SeqStack &S, DataType e){//入栈 
    if(S.top - S.base == S.stackSize){ // 栈满 
        return ERROR; 
    }
    *S.top = e;
    S.top++;
    StackTraverse(S);
    return OK;
}
  • 2> 出栈
Status Pop(SeqStack &S, DataType &e){//出栈 
    if(S.top==S.base){//栈空 
        return ERROR;
    }
    e = *(S.top-1); //返回栈顶元素的值 【易错】top所指处系栈顶元素的后一位,而非直接指向栈顶元素  
    S.top--; //【易混】要么,是先自减1,再取栈顶元素(*(S.top-1)),最后降top的位置(top--);要么,先自降top位置(top-1),再直接取当前栈顶top位置的元素(即 S.top即为栈顶元素真值) 
    return OK;
}
  • 3> 判定栈空
bool StackEmpty(SeqStack S){ // 栈空? 
    if(S.base==S.top){ // 栈空 
        return true;
    }
    return false; //栈不为空 
}
  • 3> 判定栈空
bool StackFull(SeqStack S){ //栈满? 
    if(S.top-S.base == S.stackSize){
        return true;//栈满 
    }
    return false; //栈未满 
}
  • 4> 取栈顶元素
Status GetTop(SeqStack S, DataType &e){//取栈顶元素 
    if(S.top==S.base){//栈空 
        return ERROR;
    }
    e = *(S.top-1); //【易错】
    return OK; 
}
  • 5> 栈的长度
int StackLength(SeqStack &S){
    if(S.base==NULL){
        return -1;
    }
    return S.top-S.base;
}
  • 6> 栈的遍历
Status StackTraverse(SeqStack S){//遍历
    if(S.base == NULL){ // 栈结构不存在 
        return ERROR;
    }
    if(S.base == S.top){//栈空 
        return ERROR;
    }
    DataType *p = S.top;
    while(p!=S.base){
        cout<<p<<"\t";
        p--;
    }
    cout<<p<<endl;
    return OK;
}
  • 7> 执行:Main函数
int main(){
    SeqStack S;
    InitStack(S);
    string instruction="-"; // string 在 cmd | 标准I/O 模式中输入时,将会以空格隔断字符串 
    DataType data;
    cout<<"Please Input Your Instructions:"<<endl;
    cout<"Format: \n1.\"#\":Stop Execute.\n2.\"PUSH\" 'H':Push a element 'H'. 3.\"POP\" 'T':Pop a element 'T'.\n";
    while(instruction!= "#"){
        cout<<"[INSTRUCTION]";
        cin>>instruction;
        if(instruction=="PUSH"){
            if(!StackFull(S)){
                cout<<"[PUSH DATA]";
                cin>>data;
                //cout<<"(data:"<<data<<")\n"; //for test   
                Push(S, data);
            } else {
                cout<<"<ERROR: Stack is full!So, it doesn't allow 'PUSH' operation!>"<<endl;
            }
        } else if(instruction=="POP"){
            Status status = Pop(S, data);
            if(status==OK){
                cout<<"[POP DATA]"<<data<<endl;
            } else {
                cout<<"<ERROR: 'POP' operation handle error!>"<<endl;
            }
        } else {
            cout<<"<ERROR: INSTRUCTION is error!)>"<<endl;
        }
            cout<<"(length:"<<StackLength(S)<<")\n";// for test
    }
    StackTraverse(S);
    return 0;
}
  • 8> Output
Please Input Your Instructions:
[INSTRUCTION]PUSH
[PUSH DATA]8
(length:1)
[INSTRUCTION]PUSH
[PUSH DATA]7
(length:2)
[INSTRUCTION]PUSH
[PUSH DATA]3
(length:3)
[INSTRUCTION]PUSH
[PUSH DATA]1
(length:4)
[INSTRUCTION]PUSH
[PUSH DATA]K
(length:5)
[INSTRUCTION]PUSH
[PUSH DATA]M
(length:6)
[INSTRUCTION]POP
[POP DATA]M
(length:5)
[INSTRUCTION]%
<ERROR: INSTRUCTION is error!)>
(length:5)
[INSTRUCTION]#
<ERROR: INSTRUCTION is error!)>
(length:5)
M       KM      1KM     31KM    731KM   8731KM

参考文献

  • 《数据结构(C语言版/ 严蔚敏 李冬梅 吴伟民 编)》

猜你喜欢

转载自www.cnblogs.com/johnnyzen/p/11616849.html