首页 > 代码库 > 栈基本操作(顺序栈)

栈基本操作(顺序栈)

#include<iostream>
#include<cstdlib>
using namespace std;

//定义初始化长度和每次增加的长度
const int STACK_INIT_SIZE=10;
const int STACK_INCREAMENT=2;

struct Stack{
    int* base;  //栈底
    int* top;   //栈顶
    int stacksize;  //已分配栈的大小
};

//函数声明
void show();//主界面
void InitStack(Stack &S);//初始化栈
void DestroyStack(Stack &S);//销毁栈
void ClearStack(Stack &S);//清空栈
int StackEmpty(Stack S);//判断栈是否为空
int StackLength(Stack S);//获取栈的长度
void GetTop(Stack S,int &e);//获取顶部元素
void Push(Stack &S,int e);//入栈
void Pop(Stack &S,int &e);//出栈
void StackVisit(Stack S);//从栈顶遍历栈

int main(){
    show();
    Stack s;
    int e,action;
    while(cin>>action){
        switch(action){
            case 1://初始化栈
                system("cls");
                InitStack(s);
                break;

            case 2://销毁栈
                system("cls");
                DestroyStack(s);
                break;

            case 3://清空栈
                system("cls");
                ClearStack(s);
                break;

            case 4://判断栈是否为空
                system("cls");
                StackEmpty(s);
                break;

            case 5://获取栈的长度
                system("cls");
                StackLength(s);
                break;

            case 6://获取顶部元素
                system("cls");
                GetTop(s,e);
                break;

            case 7://入栈
                system("cls");
                cout<<"please input the length you want to push"<<endl;
                int l;
                cin>>l;
                cout<<"please input the number you want to push"<<endl;
                for(int i=0;i<l;i++){
                    cin>>e;
                    Push(s,e);
                }
                break;

            case 8://出栈
                system("cls");
                Pop(s,e);
                break;

            case 9://从栈顶遍历栈
                system("cls");
                StackVisit(s);
                break;
        }
        system("pause");
        system("cls");
        show();
    }
    return 0;
}

//主界面
void show(){
    cout<<"+-------------------------------------------------+"<<endl;
    cout<<"|                                                 |"<<endl;
    cout<<"|        1 ->Initialized the stack                |"<<endl;
    cout<<"|        2 ->destroy the stack                    |"<<endl;
    cout<<"|        3 ->clear the stack                      |"<<endl;
    cout<<"|        4 ->whether the stack is empty           |"<<endl;
    cout<<"|        5 ->get the length                       |"<<endl;
    cout<<"|        6 ->get the top of stack                 |"<<endl;
    cout<<"|        7 ->push into the stack                  |"<<endl;
    cout<<"|        8 ->pop from the stack                   |"<<endl;
    cout<<"|        9 ->visit the stack                      |"<<endl;
    cout<<"|        10->quit                                 |"<<endl;
    cout<<"|                                                 |"<<endl;
    cout<<"+-------------------------------------------------+"<<endl;
}

//初始化栈
void InitStack(Stack &S){
    S.base=new int[STACK_INIT_SIZE];
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    cout<<"the stack has been initialized!"<<endl<<endl;
}

//销毁栈
void DestroyStack(Stack &S){
    delete S.base;
    cout<<"the stack has been destroyed"<<endl;
}

//清空栈
void ClearStack(Stack &S){
    S.top=S.base;
    cout<<"the stack has been cleared"<<endl;
}

//判断栈是否为空
int StackEmpty(Stack S){
    if(S.base==S.top){
        cout<<"the stack is empty!"<<endl;
        return 1;
    } else {
        cout<<"the stack is not empty!"<<endl;
        return 0;
    }
}

//获取栈的长度
int StackLength(Stack S){
    cout<<"the length of the stack is "<<S.top-S.base<<endl;
    return S.top-S.base;
}

//获取顶部元素
void GetTop(Stack S,int &e){
    if(S.top==S.base){
        cout<<"the stack is empty"<<endl;
    } else{
        e=*(S.top-1);
        cout<<"the top of the stack is "<<e<<endl;
    }
}

//入栈
void Push(Stack &S,int e){
    if(S.top-S.base>=S.stacksize){
        //在原有的空间上增加一段空间,用realloc(),具体使用方法请自行百度
        S.base=(int* )realloc(S.base,(S.stacksize+STACK_INCREAMENT)*sizeof(int));
        if(!S.base){
            cout<<"wrong!"<<endl;
        } else {
            S.top=S.base+S.stacksize;
            S.stacksize+=STACK_INCREAMENT;
        }
    }
    *(S.top++)=e;
    cout<<e<<" has been pushed into the stack"<<endl;
}

//出栈
void Pop(Stack &S,int &e){
    if(S.top==S.base){
        cout<<"the stack is empty"<<endl;
    } else {
        e=*--S.top;
        cout<<"the number has been poped,it‘s "<<e<<endl;
    }
}

//从栈顶遍历栈
void StackVisit(Stack S){
    int e;
    while(S.top>S.base){
        //方法一
        //cout<<*(--S.top)<<‘ ‘;

        //方法二
        Pop(S,e);
    }
}

 

栈基本操作(顺序栈)