标签:算法 源代码 acm c dictionary
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAXN 5000+5
#define MAXM 200+5
typedef struct Dic{
char str[MAXN];
struct Dic* next;
}Dic;
Dic *head;
char word[MAXM];
int cnt=0;
int get_word();
void convert_word();
void insert_word();
int str_cmp(char*);
int main(){
int i;
Dic* p;
freopen("data","r",stdin);
while(get_word()){
convert_word();
insert_word();
}
p=head->next;
for(i=0;i<cnt;i++){
printf("%s\n",p->str);
p=p->next;
}
return 0;
}
int get_word(){
char c;
int i=1;
while((c=getchar())!=EOF)
if(isalpha(c)){
word[0]=c;
c=getchar();
while(isalpha(c)){
word[i++]=c;
c=getchar();
}
word[i]='\0';
ungetc(c,stdin); //将新读取的第一个非字母字符退回
return 1; //读入一个单词,返回1
}
return 0; //读入结束符,返回0
}
void convert_word(){
int len=strlen(word);
int i;
for(i=0;i<len;i++)
if(isupper(word[i]))
word[i]= tolower(word[i]);
}
void insert_word(){
int i,j;
Dic *p,*temp,*q;
if(cnt==0){
head=(Dic*)malloc(sizeof(Dic));//第一个节点不放元素
p=(Dic*)malloc(sizeof(Dic));
head->next=p;
strcpy(p->str,word);
p->next=NULL;
cnt++;
}
else{
p=head;
for(i=0;i<cnt;i++)
if(str_cmp(p->next->str)<0){//插入节点
temp=(Dic*)malloc(sizeof(Dic));
temp->next=p->next;
strcpy(temp->str,word);
p->next=temp;
cnt++;
}
else if(str_cmp(p->next->str)==0) return; //重复则退出插入
else p=p->next;
temp=(Dic*)malloc(sizeof(Dic));//比所有节点都大,在最后插入
strcpy(temp->str,word);
temp->next=p->next;
p->next=temp;
cnt++;
}
return ;
}
int str_cmp(char* p){
int i;
int len=strlen(p)>strlen(word)?strlen(word):strlen(p);
for(i=0;i<=len;i++)//若比到字符串结尾仍相等,则相等
if(p[i]<word[i])
return 1; //当前节点大于要插入节点
else if(p[i]>word[i])
return -1;
return 0; //两节点相等
}Andy's First Dictionary UVA 10815,布布扣,bubuko.com
Andy's First Dictionary UVA 10815
标签:算法 源代码 acm c dictionary
原文地址:http://blog.csdn.net/u011915301/article/details/38233801