逆序一段文本算法

#include <iostream>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

struct stackNode
{
    stackNode(std::string s)
    {
        this->val=s;
        next=NULL;
    }
    std::string val;
    stackNode * next;
};

struct stringStack
{
    stringStack()
    {
        top=NULL;
    }
    bool isEmpty()
    {
        return (top==NULL);
    }
    stackNode * top;
    void push(std::string s)
    {
        stackNode * nd=new stackNode(s);
        if(top==NULL)
        {
            top=nd;
        }
        else
        {
            nd->next=top;
            top=nd;
        }
    }
    void pop()
    {
        if(top==NULL)
        {
            return;
        }
        else
        {
            stackNode *p=top;
            top=top->next;
            delete p;
        }
    }
    void printStack()
    {
        while(!isEmpty())
        {
            std::cout<<top->val<<std::endl;
            pop();
        }
    }
};

int main()
{
    stringStack sstk;
    std::string temp="";
    bool newWord=false;
    char * text="You are  son   of   a  bitch.";
    char *p=text;
    char c=*p++;
    while(c!='\0')
    {
        if(c==' ')
        {
            newWord=true;
        }
        else
        {
            newWord=false;
        }
        if(newWord==true)
        {
            if(temp.size()!=0)
            {
                sstk.push(temp);
                temp.clear();
            }
            c=*p++;
            continue;
        }
        else
        {
            temp.push_back(c);
            c=*p++;
        }
    }
    sstk.push(temp);
    sstk.printStack();
    temp.clear();
}

阅读更多

扫码向博主提问

去开通我的Chat快问

xunileida

非学,无以致疑;非问,无以广识
上一篇java常量池概念
下一篇C语言strcpy函数实现
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭