逆序一段文本算法

来源:互联网 发布:中山seo搜索排名优化 编辑:程序博客网 时间:2024/06/05 15:50
#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();}

原创粉丝点击