代码清单

 // dictionary.h
#ifndef __DICTIONARY_H__
#define __DICTIONARY_H__ #include <assert.h>
#include <stdio.h>
#include <stdio_ext.h> #include "mystring.h"
#include "linkedlist.h" void dict_init();
void dict_show(); #endif // __DICTIONARY_H__ // dictionary.c
#include "dictionary.h"
#define PATH "dictionary.dat" LinkedList list;
static void dict_load();
static void dict_store();
void dict_search(const char * eng);
void dict_add(const char * eng);
void dict_delete();
void dict_modify();
int dict_cmp(const void * s1, const void * s2); void dict_init() {
list = linkedlist_new(); dict_load();
printf("Welcome.");
} void dict_show() {
while () {
string str;
printf("\n>");
mygets(str); if (!strcmp(str, "quit;")) {
dict_store();
linkedlist_destory(&list);
printf("Bye.\n");
return;
} else if (!strcmp(str, "delete;")) {
dict_delete();
} else if (!strcmp(str, "modify;")) {
dict_modify();
} else {
dict_search(str);
}
}
} static void dict_load() {
FILE * fp;
struct Word word; while (!(fp = fopen(PATH, "rb"))) {
fp = fopen(PATH, "wb");
fclose(fp);
}
assert(fp); fread(&word, sizeof(struct Word), , fp);
while (!feof(fp)) {
linkedlist_insert(list, TRAVELDIR_BACKWARD, , word);
fread(&word, sizeof(struct Word), , fp);
} fclose(fp);
} static void dict_store() {
FILE * fp;
const int count = linkedlist_length(list); assert(fp = fopen(PATH, "wb"));
for (int i = ; i < count; i++) {
fwrite(linkedlist_get(list, TRAVELDIR_FORWARD, i + ),
sizeof(struct Word), , fp);
} fclose(fp);
} int dict_cmp(const void * s1, const void * s2) {
return strcmp(((LinkedListData *) s1)->eng, ((LinkedListData *) s2)->eng);
} void dict_search(const char * eng) {
int location;
struct Word word;
strcpy(word.eng, eng); if ((location = linkedlist_locate(list, TRAVELDIR_FORWARD, word, dict_cmp))
== -) { // not found
dict_add(eng);
} else { // found
printf("%s\n", linkedlist_get(list, TRAVELDIR_FORWARD, location)->chn);
}
} void dict_add(const char * eng) {
struct Word word;
strcpy(word.eng, eng); printf("The word does not exist, add it?\ny/n>");
if (__fpurge(stdin), getchar() == 'y') {
printf("Ok, what does it mean?\n>");
mygets(word.chn); linkedlist_insert(list, TRAVELDIR_BACKWARD, , word);
printf("The word is existed now.\n");
}
} void dict_delete() {
int location;
struct Word word; printf("What word do you wanna delete?\n>");
mygets(word.eng); if ((location = linkedlist_locate(list, TRAVELDIR_FORWARD, word, dict_cmp))
!= -) { // found
struct Word * pWord = linkedlist_get(list, TRAVELDIR_FORWARD, location); printf("Delete: %s %s\nAre you sure?\ny/n>", pWord->eng, pWord->chn);
if (__fpurge(stdin), getchar() == 'y') {
linkedlist_delete(list, TRAVELDIR_FORWARD, location);
printf("The word is deleted now.\n");
}
} else { // not found
printf("The word does not exist.\n");
}
} void dict_modify() {
int location;
struct Word word; printf("What word do you wanna modify?\n>");
mygets(word.eng); if ((location = linkedlist_locate(list, TRAVELDIR_FORWARD, word, dict_cmp))
!= -) { // found
struct Word * pWord = linkedlist_get(list, TRAVELDIR_FORWARD, location); printf("Ok, what does it mean?\n>");
mygets(pWord->chn);
printf("The word is modified now.\n");
} else { // not found
printf("The word does not exist.\n");
}
} // mystring.h
#ifndef __MYSTRING_H__
#define __MYSTRING_H__ #include <stdio.h>
#include <stdio_ext.h> #define MAX_STR_LEN 8
typedef char string[MAX_STR_LEN]; void mygets(char * s); #endif // __MYSTRING_H__ // mystring.c
#include "mystring.h" void mygets(char * s)
{
__fpurge(stdin);
fgets(s, MAX_STR_LEN, stdin);
while (*s++) {
*s = *s == '\n' ? : *s;
}
} // main.c
#include "dictionary.h" int main()
{
dict_init();
dict_show(); return ;
}

双向循环链表(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. 大话Python格式化输出字符串

    1."{},{}".format(,)用法总结: '{0},{1}'.format('var1',132908) 'var1,132908' '{},{}'.format('var ...

  2. Latex: 插入数学公式

    write equations align equations to left To only align one equation, you can \begin{flalign} &\te ...

  3. CSharpGL(42)借助帧缓存实现渲染到纹理(RenderToTexture)

    CSharpGL(42)借助帧缓存实现渲染到纹理(RenderToTexture) 渲染到纹理(Render To Texture)是实现很多OpenGL高级效果的一个基础.本文记录了如何用CShar ...

  4. iOS基于AVPlayer的视频播放

    基于 AVPlayer 自定义播放器http://www.cocoachina.com/ios/20160921/17609.html,http://www.2cto.com/kf/201608/53 ...

  5. Android使用OKHTTP解析JSON数据

    为了代码重用,我们首先封装一个类.这个类是HttpUtil HttpUtil.java package com.example.asus.networktest; import okhttp3.OkH ...

  6. Mac应用推荐

    知识管理 Outline Curio Together 开发 Clion Vim + spf13 Transmit 辅助应用 Moom PopClip Timing AppClean Markdown ...

  7. C#调用TSC条码打印机打印二维码

    #region 调用TSC打印机打印 /// <summary> /// 调用TSC打印机打印 /// </summary> /// <param name=" ...

  8. Visiual Studio CLR20r3

    问题事件名称: CLR20r3     解决方法:   步骤1:开始-->所有程序-->Microsoft Visual Studio 2012-->Visual Studio To ...

  9. 超好用的memcache管理及可视化监控工具,真方便!

    memcache做为主流的缓存数据库之一,广泛在各互联网平台使用,但是大家使用中都知道memcache目前没有一个比较好用的可视化客户端工具,每次都要输入命令进行操作,十分不方便.  而另一款主流缓存 ...

  10. VB6获取Chrome地址栏的URL信息

    上篇写到了获取IE8浏览器URL的一般方法,那这篇就写下chrome的URL怎么获取.事实上,早期的chrome版本可以通过跟IE8差不多方式获取到URL信息.但是,现在chrome的控件都是Dire ...