代码清单

  1. // dictionary.h
  2. #ifndef __DICTIONARY_H__
  3. #define __DICTIONARY_H__
  4.  
  5. #include <assert.h>
  6. #include <stdio.h>
  7. #include <stdio_ext.h>
  8.  
  9. #include "mystring.h"
  10. #include "linkedlist.h"
  11.  
  12. void dict_init();
  13. void dict_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 void dict_load();
  23. static void dict_store();
  24. void dict_search(const char * eng);
  25. void dict_add(const char * eng);
  26. void dict_delete();
  27. void dict_modify();
  28. int dict_cmp(const void * s1, const void * s2);
  29.  
  30. void dict_init() {
  31. list = linkedlist_new();
  32.  
  33. dict_load();
  34. printf("Welcome.");
  35. }
  36.  
  37. void dict_show() {
  38. while () {
  39. string str;
  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 void dict_load() {
  59. FILE * fp;
  60. struct Word 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), , fp);
  69. while (!feof(fp)) {
  70. linkedlist_insert(list, TRAVELDIR_BACKWARD, , word);
  71. fread(&word, sizeof(struct Word), , fp);
  72. }
  73.  
  74. fclose(fp);
  75. }
  76.  
  77. static void dict_store() {
  78. FILE * fp;
  79. const int count = linkedlist_length(list);
  80.  
  81. assert(fp = fopen(PATH, "wb"));
  82. for (int i = ; i < count; i++) {
  83. fwrite(linkedlist_get(list, TRAVELDIR_FORWARD, i + ),
  84. sizeof(struct Word), , 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. int location;
  96. struct Word word;
  97. strcpy(word.eng, eng);
  98.  
  99. if ((location = linkedlist_locate(list, TRAVELDIR_FORWARD, word, dict_cmp))
  100. == -) { // 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. struct Word 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, , word);
  117. printf("The word is existed now.\n");
  118. }
  119. }
  120.  
  121. void dict_delete() {
  122. int location;
  123. struct Word 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. != -) { // 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. void dict_modify() {
  143. int location;
  144. struct Word 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. != -) { // 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 <stdio.h>
  166. #include <stdio_ext.h>
  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' ? : *s;
  184. }
  185. }
  186.  
  187. // main.c
  188. #include "dictionary.h"
  189.  
  190. int main()
  191. {
  192. dict_init();
  193. dict_show();
  194.  
  195. return ;
  196. }

双向循环链表(C语言描述)(五)的更多相关文章

  1. 一种神奇的双向循环链表C语言实现

    最近在看ucore操作系统的实验指导.里面提要一个双向循环链表的数据结构,挺有意思的. 其实这个数据结构本身并不复杂.在普通链表的基础上加一个前向指针,我们就得到了双向链表,再把头尾节点连起来就是双向 ...

  2. 带头结点的双向循环链表----------C语言

    /***************************************************** Author:Simon_Kly Version:0.1 Date: 20170520 D ...

  3. 双向循环链表(C语言描述)(四)

    下面以一个电子英汉词典程序(以下简称电子词典)为例,应用双向循环链表.分离数据结构,可以使逻辑代码独立于数据结构操作代码,程序结构更清晰,代码更简洁:电子词典的增.删.查.改操作分别对应于链表的插入. ...

  4. 双向循环链表(C语言描述)(一)

    双向循环链表是链表的一种,它的每个节点也包含数据域和指针域.为了方便程序维护,可以单独为数据域定义一种数据类型,这里以整型为例: typedef int LinkedListData; 双向循环链表( ...

  5. C语言通用双向循环链表操作函数集

    说明 相比Linux内核链表宿主结构可有多个链表结构的优点,本函数集侧重封装性和易用性,而灵活性和效率有所降低.     可基于该函数集方便地构造栈或队列集.     本函数集暂未考虑并发保护. 一  ...

  6. 1.Go语言copy函数、sort排序、双向链表、list操作和双向循环链表

    1.1.copy函数 通过copy函数可以把一个切片内容复制到另一个切片中 (1)把长切片拷贝到短切片中 package main import "fmt" func main() ...

  7. 【C语言教程】“双向循环链表”学习总结和C语言代码实现!

    双向循环链表 定义 双向循环链表和它名字的表意一样,就是把双向链表的两头连接,使其成为了一个环状链表.只需要将表中最后一个节点的next指针指向头节点,头节点的prior指针指向尾节点,链表就能成环儿 ...

  8. c语言编程之双向循环链表

    双向循环链表就是形成两个环,注意每个环的首尾相连基本就可以了. 程序中采用尾插法进行添加节点. #include<stdio.h> #include<stdlib.h> #de ...

  9. 双向循环链表涉及双向指针的基本操作(C语言)

    链表大概分为有无头指针,有无尾指针,是否循环,单向还是双向, 这些都很简单,前提是你要把指针和单链表理解透彻.这些都是基于单链表 的变形,要根据实际问题,选择链表的类型. 头指针的指针域储存着储存头节 ...

  10. c语言双向循环链表

    双向循环链表,先来说说双向链表,双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继 ...

随机推荐

  1. epoll的ET和LT模式比较 - 源码分析

    eventpoll是一种文件,它实现了一种机制利用一条rdllist队列来避免阻塞地进行poll.eventpoll归根到底还是在使用poll.而ET比LT高效,并不在于是否使用了poll,更不能说是 ...

  2. 「七天自制PHP框架」第四天:模型关联

    往期回顾:「七天自制PHP框架」第三天:PHP实现的设计模式,点击此处 原文地址:http://www.cnblogs.com/sweng/p/6624845.html,欢迎关注:编程老头 前阵子在网 ...

  3. linux虚拟机下安装samba

    默认情况下,Linux系统在默认安装中已经安装了Samba服务包的一部分 ,为了对整个过程有一个完整的了解,在此先将这部分卸载掉.使用命令 rpm -qa | grep samba ,默认情况下可以查 ...

  4. 如何解释json的字符串

    public void getToken(){ String json = getJedis().get("f2b9152f36424e8b8a454df9b50eb743"); ...

  5. 怎么用VBS脚本自动注册yy娱乐的账号

    set WshShell=WScript.CreateObject("WScript.Shell") Const user = "hugetech2" Cons ...

  6. [高并发]EntityFramework之高性能扩展

    目录 简介 读写分离 指定字段更新 事务 Entity 简介 本EF扩展插件将持续更新:开源,敏捷,高性能.(由于EF Core暂未提供方便的钩子位置,暂无EF Core版本) EntityFrame ...

  7. [转]安装PIL时注册表中找不到python2.7

    如果在win7x64安装python2.7的时候选择了all user,则安装PIL的时候会显示找不到python. 解决办法:复制下面的代码到一个.py文件并运行: # # script to re ...

  8. Apache+Tomcat实现动静分离

    完成Tomcat集群搭建后,我们只需修改两.三处即可实现动静分离. 1.将原来httpd.conf中JkMount的路由规则都放入conf/extra/httpd-urimap.conf中: /*=l ...

  9. Frameset框架集的应用

    Frameset框架集常用于写网站后台页面,大多数"T字型"布局后台页面,就是应用Frameset框架集来做的.Franeset框架集的优点是,他可以在同浏览器窗口显示不同页面内容 ...

  10. Spring Data JPA 复杂/多条件组合查询

    1: 编写DAO类或接口  dao类/接口 需继承 public interface JpaSpecificationExecutor<T> 接口: 如果需要分页,还可继承 public ...