现在的位置: 首页 > 综合 > 正文

C++之Stack

2013年10月12日 ⁄ 综合 ⁄ 共 687字 ⁄ 字号 评论关闭

#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");

}

运行:

抱歉!评论已关闭.