输入一个句子:I am a student.
输出:student a am I
#include<iostream>
#include<string>
using namespace std;
typedef struct node
{
string word;
struct node *next;
}wnode;
int main()
{
string str;
wnode *pnode,*qnode;
pnode=NULL;
qnode=NULL;
qnode=new wnode;//起初使用malloc申请的,但是各种出错,原因是结构体中用了string类,不能确定word的大小
//在对qnode->word赋值时造成非法访问
qnode->word=" ";
qnode->next=NULL;
pnode=qnode;
while(cin>>str)
{
pnode=new wnode;
pnode->word=str;
pnode->next=qnode;
qnode=pnode;
}
while(qnode->next!=NULL)
{
cout<<qnode->word<<' ';
qnode=qnode->next;
delete(pnode);//不要忘记delete资源
pnode=qnode;
}
delete(qnode);
return 0;
}