长度从大到小重复不打印长度相等则字典排序—链表插入实现
来源:互联网 发布:软件项目组人员 编辑:程序博客网 时间:2024/06/05 08:34
#include <stdio.h>#include <string.h>typedef struct _Node{int len;char *string;struct _Node *next;}Node, *pNode;void main(){char str[] = "This high-way's max speed is 80 mph,and the min speed is 60 mph";char *p, *s;p = s = str;int count = 0;Node *head = new Node;//头节点head->len = 0;head->string = NULL;head->next = NULL;while (*p){s = p;while ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') || (*p >= '0' && *p <= '9') || *p == '-' || *p == '\'')p++;int len = p - s;if (len>0){count++;Node *pn = new Node;pn->len = len;pn->string = new char[len + 1];memset(pn->string, 0, len + 1);//将pn->string的前len+1个字节都初始化为0memcpy(pn->string, s, len);//从s的起始位置复制len个字节到pn->string的内存起始位置Node *prev, *post;prev = head;post = head->next;while (post && (post->len > pn->len || (post->len == pn->len && strcmp(post->string, pn->string)<0))){//判断插入节点的位置,如果post->len > pn->len或则(post->len == pn->len && strcmp(post->string,pn->string)<0),则在pre,post向前移动, //直至不满足循环条件,跳出循环,在post前插入节点prev = post;post = post->next;}if (post && strcmp(post->string, pn->string) == 0){//如果单词重复,则删除该节点delete[]pn->string;delete pn;pn = NULL;continue;}//插入节点prev->next = pn;pn->next = post;}elsep++;}Node *ph = head->next;while (ph){printf("%s %d\n", ph->string, ph->len);ph = ph->next;}printf("Total %d words\n", count);ph = head;//释放内存while (ph){head = ph->next;delete ph;ph = head;}}//------------------容器实现--------------------------------//#include <map>//#include <vector>//#include <string>//using namespace std;//void main()//{//map<string,int> m;//vector<string> v;//char str[]="This high-way's max speed is 80 mph,and the min speed is 60 mph";////+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//char *p,*s;//p=s=str;//while(*p)//{//s=p;//while((*p>='A' && *p<='Z') || (*p>='a' && *p<='z') //|| (*p>='0' && *p<='9') ||*p=='-' || *p=='\'' || *p=='_')//{//p++;//}//int len=p-s;//if(len>0)//{//char *temp=new char[len+1];//memset(temp,0,len+1);//memcpy(temp,s,len);//string ss(temp);//delete[] temp;//m.insert(make_pair(ss,len));//}//else//p++;//}////+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//int n=m.size();//int *a=new int[n];//int k=0;//for(map<string,int>::iterator iter=m.begin();iter!=m.end();iter++)//{//printf("%s %d\n",iter->first.c_str(),iter->second);//a[k++]=iter->second;//}////+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//for(int i=0;i<n-1;i++)//{//for(int j=i+1;j<n-1;j++)//{//if(a[i]<a[j])//{//int t=a[i];//a[i]=a[j];//a[j]=t;//}//}//}////+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//k=0;//while(k<n)//{//for(map<string,int>::iterator iter=m.begin();iter!=m.end();iter++)//{//if(a[k]==iter->second)//{//v.push_back(iter->first);//iter->second=-1;//}//}//k++;//}////+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//for(vector<string>::iterator iter=v.begin();iter!=v.end();iter++)//printf("%s\n",(*iter).c_str());//return;//}
0 0
- 长度从大到小重复不打印长度相等则字典排序—链表插入实现
- 循环队列的实现,插入,删除,打印,求长度
- C/C++,数据结构实现两个链表的合并(尾插法建立单链表,求链表长度,直接插入排序)
- 求不重复最长字串长度
- java-最大不重复字符串长度
- 单链表创建、测长度、打印、删除节点、插入节点、排序、逆置
- 集合相等问题(java sort排序数组长度问题)
- 从字符串S中找出不包含重复字符的最大连续子字符串长度
- 随机生成10条不重复长度为10以内的字符串,进行排序
- 随机生成10条不重复长度为10以内的字符串,进行排序
- 判断链表是否为空、求链表长度、插入新节点、删除节点、链表排序
- C语言中,链表的创建,插入,删除,遍历,求链表长度,排序等
- 数据结构链表的操作集合(建立,遍历,插入,删除,排序,长度,空判断等)
- 链表的创建 查找 排序 插入 删除 逆序 长度 显示
- 创建链表,遍历,求长度,插入,删除,排序,是否为空
- 从尾到头输出链表、字符串;不声明变量计算字符串长度
- C++类中单链表的实现(头插、尾插、头删、尾删、指定位置插入、指定位置删除、链表长度、清空链表、链表排序)
- js实现生成一个指定长度为n且随机不重复的数组
- android SubscriptionInfo更新流程
- MongoDB 3.4 高可用集群搭建(一)单例,主从模式
- Web SQL Database 与 Indexed Database
- MongoDB 3.4 高可用集群搭建(二)replica set 副本集
- 2017.3.6~2017.3.7 Harry And Magic Box 思考记录(特别不容易)
- 长度从大到小重复不打印长度相等则字典排序—链表插入实现
- hiho#1181 欧拉路2
- CAN ack slot应用
- 去掉一行中最后一个字符
- Eclipse 远程调试
- ahalei_25.26
- isual c++ 中的stdafx.h头文件的作用
- FTP服务器搭建
- Linux命令之stty