c语言双向循环链表查找,双向循环链表(C语言描述)(五)(示例代码)

1 //dictionary.h

2 #ifndef __DICTIONARY_H__3 #define __DICTIONARY_H__

4

5 #include

6 #include

7 #include

8

9 #include "mystring.h"

10 #include "linkedlist.h"

11

12 voiddict_init();13 voiddict_show();14

15 #endif //__DICTIONARY_H__

16

17 //dictionary.c

18 #include "dictionary.h"

19 #define PATH "dictionary.dat"

20

21 LinkedList list;22 static voiddict_load();23 static voiddict_store();24 void dict_search(const char *eng);25 void dict_add(const char *eng);26 voiddict_delete();27 voiddict_modify();28 int dict_cmp(const void * s1, const void *s2);29

30 voiddict_init() {31 list =linkedlist_new();32

33 dict_load();34 printf("Welcome.");35 }36

37 voiddict_show() {38 while (1) {39 stringstr;40 printf("\n>");41 mygets(str);42

43 if (!strcmp(str, "quit;")) {44 dict_store();45 linkedlist_destory(&list);46 printf("Bye.\n");47 return;48 } else if (!strcmp(str, "delete;")) {49 dict_delete();50 } else if (!strcmp(str, "modify;")) {51 dict_modify();52 } else{53 dict_search(str);54 }55 }56 }57

58 static voiddict_load() {59 FILE *fp;60 structWord word;61

62 while (!(fp = fopen(PATH, "rb"))) {63 fp = fopen(PATH, "wb");64 fclose(fp);65 }66 assert(fp);67

68 fread(&word, sizeof(struct Word), 1, fp);69 while (!feof(fp)) {70 linkedlist_insert(list, TRAVELDIR_BACKWARD, 1, word);71 fread(&word, sizeof(struct Word), 1, fp);72 }73

74 fclose(fp);75 }76

77 static voiddict_store() {78 FILE *fp;79 const int count =linkedlist_length(list);80

81 assert(fp = fopen(PATH, "wb"));82 for (int i = 0; i < count; i++) {83 fwrite(linkedlist_get(list, TRAVELDIR_FORWARD, i + 1),84 sizeof(struct Word), 1, fp);85 }86

87 fclose(fp);88 }89

90 int dict_cmp(const void * s1, const void *s2) {91 return strcmp(((LinkedListData *) s1)->eng, ((LinkedListData *) s2)->eng);92 }93

94 void dict_search(const char *eng) {95 intlocation;96 structWord word;97 strcpy(word.eng, eng);98

99 if ((location =linkedlist_locate(list, TRAVELDIR_FORWARD, word, dict_cmp))100 == -1) { //not found

101 dict_add(eng);102 } else { //found

103 printf("%s\n", linkedlist_get(list, TRAVELDIR_FORWARD, location)->chn);104 }105 }106

107 void dict_add(const char *eng) {108 structWord word;109 strcpy(word.eng, eng);110

111 printf("The word does not exist, add it?\ny/n>");112 if (__fpurge(stdin), getchar() == ‘y‘) {113 printf("Ok, what does it mean?\n>");114 mygets(word.chn);115

116 linkedlist_insert(list, TRAVELDIR_BACKWARD, 1, word);117 printf("The word is existed now.\n");118 }119 }120

121 voiddict_delete() {122 intlocation;123 structWord word;124

125 printf("What word do you wanna delete?\n>");126 mygets(word.eng);127

128 if ((location =linkedlist_locate(list, TRAVELDIR_FORWARD, word, dict_cmp))129 != -1) { //found

130 struct Word * pWord =linkedlist_get(list, TRAVELDIR_FORWARD, location);131

132 printf("Delete: %s %s\nAre you sure?\ny/n>", pWord->eng, pWord->chn);133 if (__fpurge(stdin), getchar() == ‘y‘) {134 linkedlist_delete(list, TRAVELDIR_FORWARD, location);135 printf("The word is deleted now.\n");136 }137 } else { //not found

138 printf("The word does not exist.\n");139 }140 }141

142 voiddict_modify() {143 intlocation;144 structWord word;145

146 printf("What word do you wanna modify?\n>");147 mygets(word.eng);148

149 if ((location =linkedlist_locate(list, TRAVELDIR_FORWARD, word, dict_cmp))150 != -1) { //found

151 struct Word * pWord =linkedlist_get(list, TRAVELDIR_FORWARD, location);152

153 printf("Ok, what does it mean?\n>");154 mygets(pWord->chn);155 printf("The word is modified now.\n");156 } else { //not found

157 printf("The word does not exist.\n");158 }159 }160

161 //mystring.h

162 #ifndef __MYSTRING_H__163 #define __MYSTRING_H__

164

165 #include

166 #include

167

168 #define MAX_STR_LEN 8

169 typedef char string[MAX_STR_LEN];170

171 void mygets(char *s);172

173 #endif //__MYSTRING_H__

174

175 //mystring.c

176 #include "mystring.h"

177

178 void mygets(char *s)179 {180 __fpurge(stdin);181 fgets(s, MAX_STR_LEN, stdin);182 while (*s++) {183 *s = *s == ‘\n‘ ? 0 : *s;184 }185 }186

187 //main.c

188 #include "dictionary.h"

189

190 intmain()191 {192 dict_init();193 dict_show();194

195 return 0;196 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值