双向循环链表(C语言描述)(五)
代码清单
// 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语言描述)(五)的更多相关文章
- 一种神奇的双向循环链表C语言实现
最近在看ucore操作系统的实验指导.里面提要一个双向循环链表的数据结构,挺有意思的. 其实这个数据结构本身并不复杂.在普通链表的基础上加一个前向指针,我们就得到了双向链表,再把头尾节点连起来就是双向 ...
- 带头结点的双向循环链表----------C语言
/***************************************************** Author:Simon_Kly Version:0.1 Date: 20170520 D ...
- 双向循环链表(C语言描述)(四)
下面以一个电子英汉词典程序(以下简称电子词典)为例,应用双向循环链表.分离数据结构,可以使逻辑代码独立于数据结构操作代码,程序结构更清晰,代码更简洁:电子词典的增.删.查.改操作分别对应于链表的插入. ...
- 双向循环链表(C语言描述)(一)
双向循环链表是链表的一种,它的每个节点也包含数据域和指针域.为了方便程序维护,可以单独为数据域定义一种数据类型,这里以整型为例: typedef int LinkedListData; 双向循环链表( ...
- C语言通用双向循环链表操作函数集
说明 相比Linux内核链表宿主结构可有多个链表结构的优点,本函数集侧重封装性和易用性,而灵活性和效率有所降低. 可基于该函数集方便地构造栈或队列集. 本函数集暂未考虑并发保护. 一 ...
- 1.Go语言copy函数、sort排序、双向链表、list操作和双向循环链表
1.1.copy函数 通过copy函数可以把一个切片内容复制到另一个切片中 (1)把长切片拷贝到短切片中 package main import "fmt" func main() ...
- 【C语言教程】“双向循环链表”学习总结和C语言代码实现!
双向循环链表 定义 双向循环链表和它名字的表意一样,就是把双向链表的两头连接,使其成为了一个环状链表.只需要将表中最后一个节点的next指针指向头节点,头节点的prior指针指向尾节点,链表就能成环儿 ...
- c语言编程之双向循环链表
双向循环链表就是形成两个环,注意每个环的首尾相连基本就可以了. 程序中采用尾插法进行添加节点. #include<stdio.h> #include<stdlib.h> #de ...
- 双向循环链表涉及双向指针的基本操作(C语言)
链表大概分为有无头指针,有无尾指针,是否循环,单向还是双向, 这些都很简单,前提是你要把指针和单链表理解透彻.这些都是基于单链表 的变形,要根据实际问题,选择链表的类型. 头指针的指针域储存着储存头节 ...
- c语言双向循环链表
双向循环链表,先来说说双向链表,双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继 ...
随机推荐
- head first python helloword
如何使用python 打出hello word 在python shell 键入print 'hello word'( python 2) or print ("hello word&qu ...
- 移动webAPP前端开发技巧汇总2
一.关于单位的使用 可能在传统的PC端来说,1px=1px的比例.而在移动端却不是这样,1px = ?. 因为出现了一个像素密度这样个东西,就不能在移动端使用“PX”这个单位.可能在你的大屏手机是1p ...
- java源码学习(四)ArrayList
ArrayList ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长,类似于C语言中的动态申请内存,动态增长内存. ArrayList不是线程安全的,只能用在单线程环境下, ...
- jquery获取当前选项的属性值a
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- jenkins构建后操作添加“Publish to Subversion repository”与Eclipse更新提交SVN文件冲突
jenkins配置环境信息: 1.安装“SVN Publisher plugin”插件: 2.在系统管理-系统设置中“Global SVN Publisher Settings” 填写信息:
- 跨域,json与jsonp格式
好久都没有写随笔了,最近大家都忙着考试要放假了,我也要忙一忙喽.....不过再忙我还是来啦 简单的说,json是一种轻量级的数据交换格式.平时我们使用ajax等使用的一种数据形式,那么今天就说说jso ...
- Android Shape Divider
安卓框架提供了一种LinearLayout 内部布局元素分割线的实现,建立一个指定长宽的矩形Shape: <?xml version="1.0" encoding=" ...
- Ionic开发笔记
Ionic 开发笔记 记录开发中遇到的一些问题 ion-side-menu,使所有顶部导航标题居中 <!-- 添加 align-title="center" 使顶部导航标题居 ...
- 如何将App程序发布到苹果App Store
原文网上抄录 发布步骤登陆苹果开发者中心http://developer.apple.com(99美元账号)进入itunes connect选择Manage Your Apps选择Add New Ap ...
- 使用JS开发桌面端应用程序NW.js-3-开发问题小记
前言 因为我们的项目是2C的,而XP系统是最大的用户量占比,所以只能使用nw开发而不能用Electron,本文谨记开发nw过程中遇到的各种问题以及解决方案. nw.Window.open打开新窗口不能 ...