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 实例.当打开新文档时,文档在新窗口 ...
随机推荐
- POJ 3076 / ZOJ 3122 Sudoku(DLX)
Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...
- winform 根据两点求出线上所有点及画出这条线
找出所有点: 根据斜率按照一个方向递增,求出对应的另一个方向的整数值. Point pStart = new Point(0, 2); Point pEnd = new Point(8, 2); // ...
- oracle带条件的Insert语句
背景 在一条记录完结时,自动向表中加入一条新的记录,采用的是事务处理,修改现有记录,并新增一条记录,直接采用的insert语句会报错 //主键冲突 unique constraint (XXXXXX) ...
- POI读取带有公式的Excel单元格-xssf
if(CellType.FORMULA == row.getCell(j).getCellTypeEnum()) { try { cellValue = String.valueOf(row.getC ...
- 【转】GOOGLE-PROTOBUF与FLATBUFFERS数据的序列化和反序列化
转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/unity3d-game/1607.html 关于Protobuf 通过本文的转载和分享的相关链接,足够 ...
- zufe 蓝桥选拔
https://zufeoj.com/contest.php?cid=1483 问题 A: A 代码: #include <bits/stdc++.h> using namespace s ...
- PAT 甲级 1003 Emergency
https://pintia.cn/problem-sets/994805342720868352/problems/994805523835109376 As an emergency rescue ...
- el-input为数字时验证问题
el-input为数字时,初始有值,怎么还会验证不能为空? html: <el-form-item label="审核数量:" prop="checkNum&quo ...
- maven release版本重复上传error
A couple things I can think of: user credentials are wrong url to server is wrong user does not have ...
- vThunder 安装
vThunder 安装 安装镜像下载地址 https://glm.a10networks.com/downloads A10 全球授权许可管理 https://glm.a10networks.com ...