栈的更简洁的操作

2019-07-26  本文已影响0人  始于尘埃
//顺序栈的基本操作 - 简单的排队操作 
#include <iostream>
#include <string>
using namespace std;

typedef int Elemtype;
#define Maxsize 100
typedef struct{
    string Name;
    Elemtype Num;
}People;
typedef struct{
    People *top;
    People *base;  //设置基地(栈顶、栈尾) 
    int stackSize; //栈的最大容量 
}Stack;  

//初始化
bool InitStack(Stack &S){
    S.base = new People[Maxsize];
    if(!S.base) exit(0); 
    S.top = S.base;
    S.stackSize = Maxsize;
}



//入栈 
bool Push(Stack &S,string name,Elemtype num){
    if(S.top - S.base == S.stackSize) return 0; //栈满
    S.top->Name =  name;
    S.top->Num =  num;
    S.top++;
    cout<<"成功入栈"; 
    
}


//出栈 
bool Pop(Stack &S){
    string name;
    Elemtype num;
    if(S.top == S.base) return 0;
    S.top--;
    name = S.top->Name;
    num = S.top->Num;
    cout<<num<<num<<endl;
    cout<<"成功出栈";
}

int main(){
    Stack s;
    InitStack(s);
    int i = 0,j = 0;
    string name;
    int num;
    for(i;i<3;i++){
        cout<<"请输入:\n"; 
        cin>>name>>num;
        Push(s,name,num);
    }
    for(j;j<3;j++){
        Pop(s);
    }
    return 0;
}

上一篇下一篇

猜你喜欢

热点阅读