`
jakielong
  • 浏览: 223276 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

定长队列写入文件并读取

阅读更多

#include "stdio.h"
 #include "stdlib.h"
 #include "string.h"
 
#define SIZE 5;
 #define LEN sizeof(struct node)
 
typedef struct node {
 char name[20];
 struct node *next;
 } nodes;
 
nodes *createNewNodes(void) {
 printf("创建新链表\n");
 nodes *p;
 if ((p = (nodes *) malloc(sizeof(nodes))) == NULL) {
 printf("out of mem!");
 exit(1);
 }
 
p->name[0] = '\0';
 p->next = NULL;
 return (p);
 }
 
nodes *getLastNode(nodes *p) {
 nodes *s;
 s = p->next;
 while (s != NULL) {
 s = s->next;
 }
 return (s);
 }
 
int getNodesNum(nodes *p) {
 int i = 0;
 p = p->next;
 while (p) {
 i++;
 p = p->next;
 }
 return i;
 }
 


void printAll(nodes *p, nodes *lastSecond, int n) {
 nodes *t;
 if (p->next == NULL) {
 printf("当前链表没有节点\n");
 } else {
 t = p->next;
 printf("=====链表地址情况:共%d个节点=====\n", n);
 while (t != NULL) {
 printf("数据:%s ", t->name);
 printf("地址:%p\n", t);
 t = t->next;
 }
 printf("倒数第二个元素为:%s\n", lastSecond->name);
 printf("============================\n");
 }
 }
 
nodes *testInit() {
 nodes *tmp, *p;
 p = createNewNodes();
 int i;
 char *test[5] = { "abc", "def", "ghi", "jkl", "mnt" };
 for (i = 0; i < 5; i++) {
 if ((tmp = (nodes *) malloc(sizeof(nodes))) == NULL) {
 printf("out of mem!");
 exit(1);
 }
 strncpy(tmp->name, test[i], 20);
 tmp->next = p->next;
 p->next = tmp;
 }
 return (p);
 }
 
void WriteNodesToFile(char *filename, nodes *p) {
 printf("把链表写入文件\n");
 FILE *fp;
 if ((fp = fopen(filename, "wb")) == NULL) //打开文件名为:file_name的文件
 {
 printf("can not open the file!\n");
 }
 p = p->next;
 while (p != NULL) {
 fwrite(p, LEN, 1, fp);
 p = p->next;
 }
 fclose(fp);
 }
 
int headAppend(nodes *p, char *name, nodes **lastSecond,int *nums)
 {
 printf("写入节点: %s\n", name);
 s = p;
 if ((new = (nodes *) malloc(sizeof(nodes))) == NULL) {
 printf("out of mem!");
 exit(1);
 }
 strncpy(new->name, name, 20);
 
new->next = s->next;
 s->next = new;
 
if (*nums < 5){
 *nums +=1;
 }else{
 (*lastSecond)->next = NULL;
 }
 return 1;
 }
 
void loadNodeFromFile(char *filename, nodes *head, nodes **lastSecond,int *nums) //尾插法
 {
 printf("从文件读入链表\n");
 FILE *fp;
 nodes *p, *t;
 int i = 0;
 if ((fp = fopen(filename, "rb")) == NULL) //打开文件名为:file_name的文件
 {
 return;//文件不存在!
 }
 
t = head;
 while (!feof(fp)) {
 if ((p = (nodes *) malloc(sizeof(nodes))) == NULL) {
 printf("out of mem!");
 }
 if (!fread(p, LEN, 1, fp)) {
 break;
 }
 i++;
 *nums = i;
 *lastSecond = t;
 p->next = t->next;
 t->next = p;
 t = p;
 
}
 fclose(fp);
 }
 
int main(int argc, char **argv) {
 char *filename = "file_name.txt";
 
/*第二次的时候可以尝试从这里开始注释*/
 /*
 nodes *p;
 printf("初始化链表: \n");
 p=testInit();//初始化,这里是测试的,应该是从file文件读取结构存放到指针p
 printAll(p);
 
headAppend(p,"opq");//加入一个节点
 printAll(p);
 

headAppend(p,"rst");//加入一个节点
 printAll(p);
 

//WriteNodesToFile(filename,p);
 free(p);*/
 /*一直到这里*/
 
nodes *t = createNewNodes()/*建立一个链表并初始化*/;
 nodes *lastSecond;/*指向链表的倒数第二个元素*/
 int nodeNum;/*链表个数*/
 loadNodeFromFile(filename, t, &lastSecond, &nodeNum);
 printAll(t, lastSecond, nodeNum);
 
//printf("%d",last->next==null);
 headAppend(t, "uvw",&lastSecond, &nodeNum);//加入一个节点
 printAll(t, lastSecond, nodeNum);
 
headAppend(t, "zxy",&lastSecond, &nodeNum);//加入一个节点
 printAll(t, lastSecond, nodeNum);
 /*
 
headAppend(t,"uvw");//加入一个节点
 printAll(t);
 
WriteNodesToFile(filename,t);*/
 free(t);
 return 0;
 }
 
分享到:
评论

相关推荐

    mysql数据库my.cnf配置文件

    # 每次事务提交的时候将数据写入事务日志,而这里的写入仅是调用了文件系统的写入操作,而文件系统是有 缓存的,所以这个写入并不能保证数据已经写入到物理磁盘 # 默认值1是为了保证完整的ACID。当然,你可以将这个...

    中文API支持库(1.0-0

    注解:如与远程系统建立了文件映射,那么虽然这个函数可保证数据已在当前系统写入,但不能保证数据实际写入远程系统的磁盘——除非用FILE_FLAG_WRITE_THROUGH 选项打开文件。该选项的作用是禁止写延迟,所有更新的...

    入门学习Linux常用必会60个命令实例详解doc/txt

    -n:一般而言,mount挂上后会在/etc/mtab中写入一笔资料,在系统中没有可写入文件系统的情况下,可以用这个选项取消这个动作。 4.应用技巧 在Linux 和Unix系统上,所有文件都是作为一个大型树(以/为根)的一部分...

    powerbuilder

    需要注意的是,在Windows95和Windows NT 3.51以后的版本中,使用PrintSetup()函数设置的打印机设置只对当前应用起作用,并不影响其它应用的打印机设置。对Windows 3.1来说,使用PrintSetup()函数设置的打印机设置...

    C 开发金典

    配书光盘Readme文件 C 语言通用范例开发金典 第1章 数据结构. 1 1.1 数组和字符串 2 1.1.1 一维数组的倒置 2 范例1-1 一维数组的倒置 2 ∷相关函数:fun函数 1.1.2 一维数组应用 3 范例1-2 一维...

    C语言通用范例开发金典.part2.rar

    范例1-33 限制链表长度建立长单链表 77 ∷相关函数:createlist函数 1.3.3 尾插法建立单链表 79 范例1-34 尾插法建立单链表 79 ∷相关函数:createlist函数 1.3.4 按序号查找单链表 80 范例1-35 按序号查找...

    C语言通用范例开发金典.part1.rar

    范例1-33 限制链表长度建立长单链表 77 ∷相关函数:createlist函数 1.3.3 尾插法建立单链表 79 范例1-34 尾插法建立单链表 79 ∷相关函数:createlist函数 1.3.4 按序号查找单链表 80 范例1-35 按序号查找...

    winrar3.7 Beta8

    &lt;br&gt; * WinRAR 对 RAR 和 ZIP 2.0 格式压缩文件提供完整支持,并可以解压 CAB、 ARJ、LZH、TAR、GZ、ACE、UUE、BZ2、JAR、ISO、Z、7Z 等压缩文件。 &lt;br&gt; * WinRAR 支持 NTFS 的文件安全和数据流。 ...

    jstack生成的Thread Dump日志.docx

    一般指该线程正在执行状态中,该线程占用了资源,正在处理某个操作,如通过SQL语句查询数据库、对某个文件进行写入等。 blocked 线程正处于阻塞状态,指当前线程执行过程中,所需要的资源长时间等待却一直未能...

    Oracle9i的init.ora参数中文说明

    请注意所有用户均可读取或写入 UTL_FILE_DIR 参数中指定的所有文件。 值范围: 任何有效的目录路径。 默认值: 无 plsql_v2_compatibility: 说明: 设置 PL/SQL 兼容级。如果设置为 FALSE, 将执行 PL/SQL V3 行为, ...

    补丁模块(带源码)InlinePatch,Hook,内存DLL注入等等

    子程序 inline_patch1, 逻辑型, 公开, patch尚未运行的,没加壳的程序,并运行它 .参数 文件名, 文本型, , 文件全路径 .参数 地址, 整数型, , 如Hex2Dec (“00401000”) .参数 数据, 字节集, , 如 Hex2Bin (...

    C++MFC教程

    2、开发注重交互的简单应用程序可以使用对话框为基础的窗口,如果文件读写简单这可利用CFile进行。 3、开发注重交互并且文件读写复杂的的简单应用程序可以利用以CFormView为基础视的单文档视结构。 4、利用对话框...

    下面是一系列蓝屏中可能出现的代码

    206 0x000000CE 文件名称或扩展名太长。 207 0x000000CF ring 2 堆栈使用中。 207 0x000000CF ring 2 堆栈使用中。 208 0x000000D0 输入的通用档名字元 * 或 ? 不正确, 或指定太多的通用档名字元。 209 0x...

    windows蓝屏错误代码

    206 0×000000CE 文件名称或扩展名太长。 207 0×000000CF ring 2 堆栈使用中。 207 0×000000CF ring 2 堆栈使用中。 208 0×000000D0 输入的通用档名字元 * 或 ? 不正确, 或指定太多的通用档名字元。 209 0×...

    岳维功 ortp-realease.pdf

    一个是队列,在文件 文件中。链表的实现比较简单, 队列的实现相对较复杂一点。首先,队列数据结构由三部分组成:队列头、消息块以及数据 块,图示如下: g stopper gb g count b prev b next b cont datat datab b rptr ...

Global site tag (gtag.js) - Google Analytics