单词分类排序-基于二维链表
来源:互联网 发布:永远神剑2知乎 编辑:程序博客网 时间:2024/04/27 20:29
问题来源:http://topic.csdn.net/u/20110702/19/572f6747-20cb-45ac-8efc-b694820ded26.html?seed=158117872&r=74178770#r_74178770
每一行存储个数相同的字符串,行与行之间根据单词长度从大到小。
存储结构是个二维链表。
#include <stdio.h>#include <stdlib.h>#include <string.h>#define SIZE 100struct RowNode {char str[SIZE];RowNode* next;};struct ORNode{int strLen;RowNode* rowHead;ORNode* next;};void InitRowNode(RowNode* & node){memset(node->str, 0, SIZE);node->next = NULL;}void InitORNode(ORNode* & node){node->strLen = 0;node->rowHead = NULL;node->next = NULL;}/*** 插入新单词 */void InsertWord(ORNode* & head, char* word){// 查找列ORNode* pLine = NULL;ORNode* preLine = NULL;ORNode* newLine = NULL;RowNode* pRow = NULL;RowNode* preNode = NULL;RowNode* pNode = (RowNode*)malloc( sizeof(RowNode) );InitRowNode(pNode);strcpy(pNode->str, word);for (pLine = head; pLine && (strlen(word)>pLine->strLen) ; preLine = pLine, pLine = pLine->next);// pLine ==NULL时候:head为NULL 或者 word长度小于所有行的长度if ( pLine==NULL || strlen(word)<pLine->strLen )// 新建一行插入{newLine = (ORNode*)malloc( sizeof(ORNode) );InitORNode(newLine);newLine->strLen = strlen(word);newLine->rowHead = pNode;if (preLine==NULL)// 直接插在头结点位置{newLine->next = head;head = newLine;}else// 插入preLine后一行{newLine->next = preLine->next;preLine->next = newLine;}}else// 插入pLine指向的行{// pLine->rowHead为当前行// 把word按照字典顺序插入该行for (pRow = pLine->rowHead; pRow; preNode = pRow, pRow = pRow->next){if ( strcmp(word, pRow->str)<0 )break;}if (preNode==NULL)// 插入头部{pNode->next = pLine->rowHead;pLine->rowHead = pNode;}else// 插入preNode的后面{pNode->next = preNode->next;preNode->next = pNode;}}}/*** 按行打印单词 */void PrintWordsByLine(ORNode* head){ORNode* pIter = head;RowNode* pRowIter = NULL;while (pIter){printf( "word of %d letter(s) :\n", pIter->strLen);pRowIter = pIter->rowHead;while (pRowIter){printf("%s\n", pRowIter->str);pRowIter = pRowIter->next;}pIter = pIter->next;}}/*** 销毁十字链表 */void DestroyOrthoList(ORNode* &head){ORNode* pIter = head;ORNode* pWork = NULL;RowNode* pRowIter = NULL;RowNode* pRowWork = NULL;while (pIter){// 销毁行pRowIter = pIter->rowHead;while (pRowIter){pRowWork = pRowIter;pRowIter = pRowIter->next;free(pRowWork);pRowWork = NULL;}// 销毁该结点pWork = pIter;pIter = pIter->next;free(pWork);pWork = NULL;}}int main(){FILE* fp = fopen("input.txt", "r+");if (fp==NULL)exit(0);ORNode* head = NULL;int count=0;char str[SIZE];memset(str, 0, SIZE);while ( fscanf(fp, "%s", str)==1 ){InsertWord(head, str);++count;memset(str, 0, SIZE);}PrintWordsByLine(head);DestroyOrthoList(head);fclose(fp);system("PAUSE");return 0;}
测试文件内容 input.txt:
void PrintWordsByLine(ORNode* head){ORNode* pIter = head;RowNode* pRowIter = NULL;while (pIter){printf( "word of %d letter(s) :\n", pIter->strLen);pRowIter = pIter->rowHead;while (pRowIter){printf("%s\n", pRowIter->str);pRowIter = pRowIter->next;}pIter = pIter->next;}}统计PrintWordsByLine函数这段代码,输出结果:
word of 1 letter(s) : = = = = = { { { } } }word of 2 letter(s) : %d ofword of 4 letter(s) : voidword of 5 letter(s) : "word :\n", NULL; head) head; pIter pIter while whileword of 7 letter(s) : (pIter) ORNode* printf(word of 8 letter(s) : RowNode* pRowIter pRowIter pRowIterword of 9 letter(s) : letter(s)word of 10 letter(s) : (pRowIter)word of 12 letter(s) : pIter->next;word of 14 letter(s) : printf("%s\n",word of 15 letter(s) : pIter->rowHead; pIter->strLen); pRowIter->next; pRowIter->str);word of 24 letter(s) : PrintWordsByLine(ORNode*请按任意键继续. . .
示例存储结构图:
同时 参见之前一篇blog 基于C++的十字链表实现:http://blog.csdn.net/dizuo/article/details/6361077
- 单词分类排序-基于二维链表
- 【排序】单词分类(word.c/cpp)
- C++链表的分类排序
- 单词排序
- 单词排序
- 单词排序
- 单词排序
- 单词排序
- 单词排序
- 单词排序
- 单词排序
- 高中英语单词名词分类
- 小学英语单词分类汇总
- 小学英语单词分类集
- 【单词分类】解题报告
- 分类记单词
- 单词分类记忆汇总
- 单词分类 (Standard IO)
- Java类加载原理解析
- poj Jury Compromise
- List
- 新博客上线了,放下手里的代码,来写点
- 小小感怀
- 单词分类排序-基于二维链表
- 解决国内域名必须备案,国外访问过慢的问题
- 如何让Android开机不锁屏
- tsocks 快速使用配置
- .net 开源项目资源收集
- android 学习讨论网站 .
- poj3126
- vector 内存泄露问题
- nfs 快速使用配置