#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个字节都初始化为0 memcpy(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; } else p++; } 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;//}
新闻热点
疑难解答