转自:http://blog.csdn.net/echo_qiang/article/details/6233057

/*********************************************************************
*
* Filename: pfile.c
* Version: 1.0
* Description: Demo for Linux LIST utility
* Compilation: gcc –D__KERNEL__ -I/usr/src/linux/include pfile.c
* Status: Stable
* Author: Yang Shazhou<pubb@163.net>
* Created at: Thu Jul 15 13:50:33 2004
* Modified at: Thu Jul 15 14:39:03 2004
* Modified by: Yang Shazhou<pubb@163.net>
*
* Copyright (c) 2004 Yang Shazhou, All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
********************************************************************/
#include <linux/list.h>
#include <stdio.h>
#include <strings.h> int main(int argc,char *argv[])
{
LIST_HEAD(list); //定义存放文件内容的链表
FILE *fp; struct file_store {
char c;
struct list_head node;
} *pstore; if(argc<){
printf("usage: pfile <filename> <[r]>/n");
return -;
}
if(!(fp=fopen(argv[],"rb"))){
printf("fopen(%s) error/n",argv[]);
return -;
} /* 读文件到链表 */
while(){
if(!(pstore=(struct file_store *)malloc(sizeof(struct file_store))))
break;
pstore->c=fgetc(fp);
if(feof(fp)){
free(pstore);
break;
}
list_add_tail(&pstore->node,&list); //将本字符插入链表中
}
fclose(fp); /* 遍历链表,输出链表中的节点个数,即文件字符数 */
int count=;
struct list_head *p;
list_for_each(p,&list){
count++;
}
printf("%s has altogether %d character(s)/n",argv[],count); /* 根据命令行参数正向/反向遍历链表,输出链表中存放的字符,同时释放各节点 */
if(argc> && !strcasecmp(argv[],"r")){
struct list_head *p;
list_for_each_entry_reverse(pstore,&list,node){ //反向遍历,没有保护
p=pstore->node.next;
list_del(&pstore->node);
putchar(pstore->c);
free(pstore);
/* 如果没有这一句,将报segmentation fault */
pstore=list_entry(p,struct file_store,node); //取数据项
}
}else{
struct file_store *p;
list_for_each_entry_safe(pstore,p,&list,node){ //正向遍历,有保护
list_del(&pstore->node);
putchar(pstore->c);
free(pstore);
}
} return ;
}

linux 链表实例应用程序【转】的更多相关文章

  1. Linux Epoll介绍和程序实例

    Linux Epoll介绍和程序实例 1. Epoll是何方神圣? Epoll但是当前在Linux下开发大规模并发网络程序的热门人选,Epoll 在Linux2.6内核中正式引入,和select类似, ...

  2. 35、在编译Linux内核中增加程序需要完成以下3项工作

    在编译Linux内核中增加程序需要完成以下3项工作: 将编写的源代码拷入Linux内核源代码的相应目录. 在目录的Kconfig文件中增加关于新源代码对应项目的编译配置选项 在目录的Makefile文 ...

  3. dvm 的进程和 Linux 的进程, 应用程序的进程是否为同一个概念?

    dvm 指 dalvik 的虚拟机. 每一个 Android 应用程序都拥有一个独立的 Dalvik 虚拟机实例,应用程序都在它自己的进程中运行.而每一个 dvm 都是在 Linux 中的一个进程,所 ...

  4. Linux多线程实例练习 - pthread_cancel()

    Linux多线程实例练习 - pthread_cancel 1.代码 xx_pthread_cancel.c #include <pthread.h> #include <stdio ...

  5. Linux多线程实例练习 - pthread_exit() 与 pthread_join()

    Linux多线程实例练习 - pthread_exit 与 pthread_join pthread_exit():终止当前线程 void pthread_exit(void* retval); pt ...

  6. Linux多线程实例练习 - pthread_create()

    Linux多线程实例练习 pthread_create():创建一个线程 int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, ...

  7. Linux Bash命令关于程序调试详解

    转载:http://os.51cto.com/art/201006/207230.htm 参考:<Linux shell 脚本攻略>Page22-23 Linux bash程序在程序员的使 ...

  8. Linux经久不衰的应用程序

    Linux里面的应用程序一贯以高安全性,高性价比(功能/所占空间),此次记录一下Linux里面比较常用的而且经久不衰的应用程序. Shell:               bash(它结合了 csh ...

  9. 使用 WPF 创建单实例应用程序

    一个简单的例子就是大家在使用很多应用程序,例如在使用Microsoft Word 时会遇到一种情况,不管你打开多少个文档,系统一次只能加载一个winword.exe 实例.当打开新文档时,文档在新窗口 ...

随机推荐

  1. 前端----css总结

    1,权重计算: 权重:id  class  标签--->>>顺序不变 当权重一样时,显示后来设置的 继承下来的标签,权重为0 若权重为0,那么谁描述的近,就显示谁 若权重为0,描述的 ...

  2. BZOJ 4012 HNOI2015 开店 树的边分治+分治树

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4012 题意概述:给出一颗N点的树,保证树上所有点的度不超过3,树上每个点有权值,每条边有权 ...

  3. NO2——最短路径

    [Dijkstra算法] 复杂度O(n2) 权值必须非负 /* 求出点beg到所有点的最短路径 */ // 邻接矩阵形式 // n:图的顶点数 // cost[][]:邻接矩阵 // pre[i]记录 ...

  4. (转)mongdb性能优化收集

    一.数据库最大连接数问题当你在后台日志中,发现大量“connection refused because too many open connections: 819”信息时,一般跟你没有设置合适的最 ...

  5. [CCF] 201612-2 工资计算

    [思路]按照题意对初始工资S进行循环,计算缴税后工资,若与T相等则退出循环,输出结果. #include <iostream> #include <windows.h> usi ...

  6. Netscaler工作流程

    Netscaler工作流程 http://blog.51cto.com/caojin/1898310 Citrix Netscaler有很多功能模块来满足应用交付的需求,为了能够做好的配置和排错工作, ...

  7. [TJOI2018]数学计算 线段树

    ---题面--- 题解: ,,,考场上看到这题,没想到竟然是省选原题QAQ,考场上把它当数学题想了好久,因为不知道怎么处理有些数没有逆元的问题....知道这是线段树后恍然大悟. 首先可以一开始就建出一 ...

  8. C++的一些小操作、常用库及函数(持续更新)

    1. 强制保留n位小数(位数不足则强制补零) 头文件: #include <iomanip> 在输出前: cout<<setprecision(n); 也有不用头文件的方式,在 ...

  9. BZOJ1452 [JSOI2009]Count 【树套树 (树状数组)】

    1452: [JSOI2009]Count Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 2693  Solved: 1574 [Submit][St ...

  10. MySQL使用笔记(四)数据的操作

    By francis_hao    Dec 14,2016 数据的操作包括插入数据记录.更新数据记录和删除数据记录. 插入数据记录 插入单条数据记录 field表示的字段名和value表示数据要一一对 ...