C++之Stack

来源:互联网 发布:剑三捏脸成男数据 贴 编辑:程序博客网 时间:2024/05/01 09:49
#include "iostream"
using namespace std;

class Cstack
{
    int *top;
    int *bottom;
public:
    Cstack()
    {   top = bottom = new int[100];   }
    void Push(int c)
    {
        if ((top - bottom) < 100)
            *top ++=c;
    }
    
    int Pop()
    {
        if(--top >= bottom)
        return *top;
    }
    
    ~Cstack()
    {   delete top;}
};

char * ReverseName(char *name)
{
    Cstack s;
    char *reverse;
    for (int i=0; i<strlen(name); i++)
        s.Push(name[i]);
        
    reverse = new char[strlen(name) + 1];
    for(int i = 0; i < strlen(name); i++)
        reverse[i] = s.Pop();
     reverse[strlen(name)] = '\0';
     
    return reverse;
}

void main()
{
    char str[20];
    cout << "输入字符串:  ";
    cin >> str;
    cout << "该字符串的反序为:  "<<ReverseName(str)<<'\n';
    
    system("pause");

}


运行: