链表单词字典顺序c语言,以链表方式实现字符串中的单词顺序反转

// list.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include "string"

#include "iostream"

using namespace std;

//申明结构体变量,链表中每个结构体包括单词部分,与链接指针部分

struct wordElem

{

string word;

wordElem  *pre;

};

int _tmain(int argc, _TCHAR* argv[])

{

//由于cin不能读入空格或换行等字符,故在读入前需先做如下语句的申明

cin.unsetf(ios::skipws);

char input;

string word;

string blanks;

wordElem *head;

wordElem *Current;

wordElem *tempElem;

head = new wordElem;

head->pre = NULL;

Current = head;

cout<

cin>>input;

//读入字符串,并将有效单词存入链表节点中,链表中单词以原字符串的反向顺序保存有效单词

while (input!= '\n')

{

//读入有效单词存储在word变量中

while ((input >= 'a'&&input <= 'z') || (input >= 'A'&&input <= 'Z') || (input >= '0'&&input<= '9'))

{

word += input;

cin>>input;

if (input == '\n')

{

break;

}

}

//将有效单词存入链表节点中

if (word.length() > 0)

{

tempElem = new wordElem;

tempElem->word = word;

tempElem->pre = Current;

Current = tempElem;

word.clear();

}

//读入分隔符,如空格符或标点符号,忽视分隔符

while (input!='\n' &&!((input >= 'a'&&input <= 'z') || (input >= 'A'&&input <= 'Z') || (input >= '0'&&input<= '9')))

{

blanks += input;

cin>>input;

}

}

//判断字符串是否为空

if (Current == head)

{

cout<

}

//反向输出链表里的单词

else

{

while (Current != head)

{

cout <word <

Current = Current->pre;

}

cout <

}

return 0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值