linux 链表实例应用程序【转】
转自: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 链表实例应用程序【转】的更多相关文章
- Linux Epoll介绍和程序实例
Linux Epoll介绍和程序实例 1. Epoll是何方神圣? Epoll但是当前在Linux下开发大规模并发网络程序的热门人选,Epoll 在Linux2.6内核中正式引入,和select类似, ...
- 35、在编译Linux内核中增加程序需要完成以下3项工作
在编译Linux内核中增加程序需要完成以下3项工作: 将编写的源代码拷入Linux内核源代码的相应目录. 在目录的Kconfig文件中增加关于新源代码对应项目的编译配置选项 在目录的Makefile文 ...
- dvm 的进程和 Linux 的进程, 应用程序的进程是否为同一个概念?
dvm 指 dalvik 的虚拟机. 每一个 Android 应用程序都拥有一个独立的 Dalvik 虚拟机实例,应用程序都在它自己的进程中运行.而每一个 dvm 都是在 Linux 中的一个进程,所 ...
- Linux多线程实例练习 - pthread_cancel()
Linux多线程实例练习 - pthread_cancel 1.代码 xx_pthread_cancel.c #include <pthread.h> #include <stdio ...
- Linux多线程实例练习 - pthread_exit() 与 pthread_join()
Linux多线程实例练习 - pthread_exit 与 pthread_join pthread_exit():终止当前线程 void pthread_exit(void* retval); pt ...
- Linux多线程实例练习 - pthread_create()
Linux多线程实例练习 pthread_create():创建一个线程 int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, ...
- Linux Bash命令关于程序调试详解
转载:http://os.51cto.com/art/201006/207230.htm 参考:<Linux shell 脚本攻略>Page22-23 Linux bash程序在程序员的使 ...
- Linux经久不衰的应用程序
Linux里面的应用程序一贯以高安全性,高性价比(功能/所占空间),此次记录一下Linux里面比较常用的而且经久不衰的应用程序. Shell: bash(它结合了 csh ...
- 使用 WPF 创建单实例应用程序
一个简单的例子就是大家在使用很多应用程序,例如在使用Microsoft Word 时会遇到一种情况,不管你打开多少个文档,系统一次只能加载一个winword.exe 实例.当打开新文档时,文档在新窗口 ...
随机推荐
- 牛客网暑期ACM多校训练营(第七场):J-Sudoku Subrectangles
链接:J-Sudoku Subrectangles 题意:给出 n * m 的字母矩阵,公52种字母.求出不含重复元素的子矩阵的个数. 题解: L[i][j]:s[i][j] ~ s[i][ j - ...
- AcCoder Contest-115 D - Christmas
D - Christmas Time limit : 2sec / Memory limit : 1024MB Score : 400 points Problem Statement In some ...
- mysql语法总结
增: 删: 改: 查: 索引: 建: alter table: sql一些常用的经典语句,最后是select as的用法
- HDU 4714 Tree2cycle(树状DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup)
Description A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 ...
- css3弹性盒子模型之box-flex
css3弹性盒子模型之box-flex 浏览器支持 目前没有浏览器支持 box-flex 属性. Firefox 支持替代的 -moz-box-flex 属性. Safari.Opera 以及 Chr ...
- VS2010历史记录清理
把如下粘贴到文本文件里,另存为批处理文件.(后缀为 *.bat)双击执行就可 @echo off cd \ @echo on @REG Delete HKEY_CURRENT_USER\Softwar ...
- elasticsearch 工具类
<?php /** * Created by PhpStorm. * User: brady * Date: 2018/4/8 * Time: 10:31 * esticsearch helpe ...
- 文件系统中 atime,lazytime,relatime 详聊
atime,ctime,mtime是文件inode的三个时间戳,分别表示文件最近一次的访问时间:inode本身的更改(changed)时间:文件数据的更改(modify)时间:这几个概念还是很好区分. ...
- Windows下的Memcache安装与Java部署
Windows下的Memcache安装: 1. 下载memcached的windows稳定版,解压放某个盘下面,比如在c:\memcached 2. 在终端(也即cmd命令界面)下输入 ‘c:\mem ...
- Hibernate常用方法之_删除
1.使用session的delete方法 public void deleteUser(User user){ Session session = null; Transaction transact ...