C++顺序栈

来源:互联网 发布:java 字符串乱码 编辑:程序博客网 时间:2024/05/17 23:34
#include <iostream>using namespace std;struct  SqStack{    char data[20];    int top;};void InitStack(SqStack &s){    for(int i=0; i<20; ++i)    {        s.data[i]=' ';    }    s.top=-1;}bool StackEmpty(SqStack s){    return(s.top==-1);}bool Push(SqStack& s,char& e){    if(s.top==20-1)        return false;    s.top++;    s.data[s.top]=e;    return true;}bool Pop(SqStack& s,char& e){    if(s.top==-1)        return false;    e=s.data[s.top];    s.top--;    return true;}bool GetTop(SqStack s,char& e){    if(s.top==-1)        return false;    e=s.data[s.top];    return true;}void DestroyStack(SqStack &s){    delete []s.data;}int main(){    SqStack L;    InitStack(L);    if(StackEmpty(L))        cout<<"空栈。\n";    else        cout<<"非空栈。\n";    char e[5]= {'a','b','c','d','e'};    for(int i=0; i<5; ++i)    {        if(Push(L,e[i]));        else            cout<<"栈满。\n";    }    for(int i=L.top;i>=0;--i)    {        cout<<L.data[i];    }    cout<<endl;    char a;    while(Pop(L,a))        cout<<a;    cout<<endl;    cout<<"栈的长度为:"<<L.top+1<<endl;    cout<<"栈底的元素为:"<<L.data[0]<<endl;    return 0;}

0 0