二维单链表
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define LENWORD 21
typedef struct node{
char *word;
struct node *next;
}Node;
typedef struct list{
char letter;
struct list *next;
struct node *words;
}List;
char *my_fgets(char *p,int n);//字符串输入
void ClearList(List **plist);//清空List
int CheckWord(char *str);//检测字符串
int AddList(List **plist);//增加List
int Search(List **plist);//查找List
int DeleteList(List **plist);//删除List
int Showlist(List **plist);//显示List
int main(){
List *pdata=NULL;
ClearList(&pdata);
for(fputs("Input the word:->",stdout);AddList(&pdata);fputs("Input the word:->",stdout));//增加List
for(fputs("Input the word to search:->",stdout);Search(&pdata);fputs("Input the word to search:->",stdout));//查找List
for(fputs("Input the word to delete:->",stdout);DeleteList(&pdata);printf("Word:%d\n",Showlist(&pdata)),fputs(pdata?"Input the word to delete:->":"EmptyList\n",stdout));//删除List
ClearList(&pdata);
return 0;}
//
char *my_fgets(char *p,int n){
char ch=0,*input=NULL,*found=NULL;
if(input=fgets(p,n,stdin))
if(found=strchr(input,10)) *found=0;
else while((ch=getchar())!=EOF&&ch!=10);
return input;}
void ClearList(List **plist){
for(List *pltemp=NULL;pltemp=*plist;){
for(Node *pntemp=NULL;pntemp=pltemp->words;){
pltemp->words=pltemp->words->next;
free(pntemp->word);//清除node->word内存
free(pntemp);//清除node内存
}
*plist=(*plist)->next;//二级指针进,试试pltemp
free(pltemp);
}
}
int CheckWord(char *str){
char *str_copy=str;
if(!*str) return -1;
for(;*str>='A'&&*str<='z';*str=tolower(*str),str++);//转为小写字母
if(*str){
printf("\"%s\" is not a word.\n",str_copy);
return 0;
}
return 1;}
int AddList(List **plist){
char input[LENWORD]={};
Node **ptemp=NULL;
if(!my_fgets(input,LENWORD)||!*input) return 0;//读取输入
if(!CheckWord(input)) return 1;//检测是否字母
for(;*plist&&*input>(*plist)->letter;plist=&(*plist)->next);
if(!*plist||*input!=(*plist)->letter){//空链表或者未找到相同头字母,需创建新List
List *plcall=NULL;
if(!(plcall=(List *)calloc(1,sizeof(List)))){
fputs("Failed to create memory.\n",stdout);
return 0;
}
plcall->letter=*input;
plcall->next=*plist;
plcall->words=NULL;
*plist=plcall;
}
for(ptemp=&(*plist)->words;*ptemp&&strcmp(input,(*ptemp)->word)>0;ptemp=&(*ptemp)->next);//ptemp=&(*plist)->words
if(*ptemp&&!strcmp((*ptemp)->word,input)){//发现重复单词
printf("\"%s\" is a duplicate word,try again.\n",input);
return 1;
}
Node *pncall=NULL;
if(!(pncall=(Node *)calloc(1,sizeof(Node)))||!(pncall->word=(char *)calloc(strlen(input)+1,sizeof(char)))){//无需新建pscall
fputs("Failed to create memory.\n",stdout);
return 0;
}
strcpy(pncall->word,input);//用strncpy会出bug
pncall->next=*ptemp;
*ptemp=pncall;
return 1;}
int Search(List **plist){
char input[LENWORD]={};
if(!*plist||!my_fgets(input,LENWORD)||!*input) return 0;
if(!CheckWord(input)) return 1;
for(;*plist&&*input>(*plist)->letter;plist=&(*plist)->next);
if(!*plist||*input<(*plist)->letter){
printf("Couldn't found \"%s\".\n",input);
return 1;
}
Node *ptemp=(*plist)->words;
for(;ptemp&&strcmp(input,ptemp->word)>0;ptemp=ptemp->next);
if(!strcmp(input,ptemp->word)){
printf("Found \"%s\".\n",input);
return 1;
}
else{
printf("Couldn't found \"%s\".\n",input);
return 1;
}
}
int DeleteList(List **plist){
char input[LENWORD]={};
if(!*plist||!my_fgets(input,LENWORD)||!*input) return 0;
if(!CheckWord(input)) return 1;
for(;*plist&&*input>(*plist)->letter;plist=&(*plist)->next);
if(!*plist||*input<(*plist)->letter){
printf("Couldn't found \"%s\".\n",input);
return 1;
}
if(strcmp(input,(*plist)->words->word)||(*plist)->words->next){
Node **pptemp=&(*plist)->words;
for(;*pptemp&&strcmp(input,(*pptemp)->word)>0;pptemp=&(*pptemp)->next);
if(!*pptemp||strcmp(input,(*pptemp)->word)<0){
printf("Couldn't found \"%s\".\n",input);
return 1;
}
else{
Node *ptemp=(*pptemp)->next;
free((*pptemp)->word);
free(*pptemp);
*pptemp=ptemp;
}
}
else{
List *ptemp=*plist;
free(ptemp->words->word);
free(ptemp->words);
*plist=ptemp->next;
free(ptemp);
}
return 1;}
int Showlist(List **plist){
Node *ptemp=NULL;
int count=0;
for(;*plist;plist=&(*plist)->next){
fputc((*plist)->letter,stdout);
fputs(":\n",stdout);
int i=0;
for(ptemp=(*plist)->words;ptemp;printf("NO.%d:%s\n",i+1,ptemp->word),ptemp=ptemp->next,i++,count++);
}
return count;}