12.8.7-PointersOnC-20220416

二维单链表

#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;}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fleet1126

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值