举例说,Linux核心名单(两)
使用列表
我认为最好的方式,成为熟悉的核心列表功能是看一些简单的例子,素材去更好的理解链表。
以下是一个样例。包括创建。加入。删除和遍历链表。
<span style="font-size:18px;">#include <stdio.h>
#include <stdlib.h> #include "list.h" struct kool_list{
int to;
struct list_head list;
int from;
};//自己定义欲链接的数据额结构,并包括双向链表结构 int main(int argc, char **argv){ struct kool_list *tmp;
struct list_head *pos, *q;
unsigned int i; struct kool_list mylist;
INIT_LIST_HEAD(&mylist.list);//初始化一个链表表头 /* 向<span style="font-family: Arial, Helvetica, sans-serif;">mylist中加入元素</span><span style="font-family: Arial, Helvetica, sans-serif;"> */</span>
for(i=5; i!=0; --i){
tmp= (struct kool_list *)malloc(sizeof(struct kool_list)); /* INIT_LIST_HEAD(&tmp->list);
*
* this initializes a dynamically allocated list_head. we
* you can omit this if subsequent call is add_list() or
* anything along that line because the next, prev
* fields get initialized in those functions.
*/
printf("enter to and from:");
scanf("%d %d", &tmp->to, &tmp->from); /* add the new item 'tmp' to the list of items in mylist */
list_add(&(tmp->list), &(mylist.list));//项链表中加入新的元素节点,tmp中的list
/* you can also use list_add_tail() which adds new items to
* the tail end of the list
*/
}
printf("\n"); /* now you have a circularly linked list of items of type struct kool_list.
* now let us go through the items and print them out
*/ /* list_for_each() is a macro for a for loop.
* first parameter is used as the counter in for loop. in other words, inside the
* loop it points to the current item's list_head.
* second parameter is the pointer to the list. it is not manipulated by the macro.
*/
printf("traversing the list using list_for_each()\n");
list_for_each(pos, &mylist.list){//遍历链表,pos依次指向链表的元素 /* at this point: pos->next points to the next item's 'list' variable and
* pos->prev points to the previous item's 'list' variable. Here item is
* of type struct kool_list. But we need to access the item itself not the
* variable 'list' in the item! macro list_entry() does just that. See "How
* does this work? " below for an explanation of how this is done.
*/
tmp= list_entry(pos, struct kool_list, list);//获得包括pos节点的数据结构<span style="font-family: Arial, Helvetica, sans-serif;">struct kool_list指针</span> /* given a pointer to struct list_head, type of data structure it is part of,
* and it's name (struct list_head's name in the data structure) it returns a
* pointer to the data structure in which the pointer is part of.
* For example, in the above line list_entry() will return a pointer to the
* struct kool_list item it is embedded in!
*/ printf("to= %d from= %d\n", tmp->to, tmp->from); }
printf("\n");
/* since this is a circularly linked list. you can traverse the list in reverse order
* as well. all you need to do is replace 'list_for_each' with 'list_for_each_prev'
* everything else remain the same!
*
* Also you can traverse the list using list_for_each_entry() to iterate over a given
* type of entries. For example:
*/
printf("traversing the list using list_for_each_entry()\n");
list_for_each_entry(tmp, &mylist.list, list)
printf("to= %d from= %d\n", tmp->to, tmp->from);
printf("\n"); /* now let's be good and free the kool_list items. since we will be removing items
* off the list using list_del() we need to use a safer version of the list_for_each()
* macro aptly named list_for_each_safe(). Note that you MUST use this macro if the loop
* involves deletions of items (or moving items from one list to another).
*/
printf("deleting the list using list_for_each_safe()\n");
list_for_each_safe(pos, q, &mylist.list){
tmp= list_entry(pos, struct kool_list, list);
printf("freeing item to= %d from= %d\n", tmp->to, tmp->from);
list_del(pos);
free(tmp);
} return 0;
}</span>
几种常用的核心清单API实现。上述案件Linux核心名单(三)介绍。
举例说,Linux核心名单(两)的更多相关文章
- Linux中的两种守护进程stand alone和xinetd
Linux中的两种守护进程stand alone和xinetd --http://www.cnblogs.com/itech/archive/2010/12/27/1914846.html#top 一 ...
- linux核心版本号的说明
日志不会很长,因为每天都在学习,我认为的重点,我自己做的记录,我很高兴能分享给大家: Linux的核心版本编号有点类似如下癿样子: 2.6.18-92.el5 主版本.次版本.释出版本-修改版本 因为 ...
- 动态替换Linux核心函数的原理和实现
转载:https://www.ibm.com/developerworks/cn/linux/l-knldebug/ 动态替换Linux核心函数的原理和实现 在调试Linux核心模块时,有时需要能够实 ...
- bootparam - 介绍Linux核心的启动参数
描叙 Linux 核心在启动的时候可以接受指定的"命令行参数"或"启动参数".在通常情况下,由于核心有可能无法识别某些硬件,或可能将某些硬件识别为不正确的配置, ...
- Linux共享库两种加载方式简述
Linux共享库两种加载方式简述 动态库技术通常能减少程序的大小,节省空间,提高效率,具有很高的灵活性,对于升级软件版本也更加容易.与静态库不同,动态库里面的函数不是执行程序本身 的一部分,而是 ...
- Linux核心命令
Linux核心命令 strace(查看系统调用的一个过程) 例:strace cat /test.txt netstat perf top pidstat mpstat dstat vmstat sl ...
- Linux 服务管理两种方式service和systemctl
Linux 服务管理两种方式service和systemctl 1.service命令 service命令其实是去/etc/init.d目录下,去执行相关程序 # service命令启动redis脚本 ...
- 为什么空格拷贝到linux 会变成两个
为什么空格拷贝到linux 会变成两个 学习了:https://zhidao.baidu.com/question/266438357.html 在vi界面内输入:set paste 然后进行拷贝: ...
- Linux 下启动两个tomcat
Linux 下启动两个tomcat 闲来无事学习nginx,想要配置个load balance.可是先决条件是:得有两个web容器.两个电脑是不用想了.只能想办法在一个机器上启动两个tomcat.原以 ...
随机推荐
- SonarQube升级
1.阅读SonarQube更新日志: http://docs.codehaus.org/display/SONAR/Upgrading#Upgrading-ReleaseUpgradeNotes 2. ...
- JAVA card 应用开发(三) 把APPLET(CAP文件)装载到卡片
依据前面两篇博文.我们能够在Eclipse上建立好Applet.而且能够有多个AID.能够选择不同的应用. 那么,以上我们都是基于模拟环境的逻辑,实际上有些函数接口是须要实际的环境.就是说我们须要把A ...
- 服务器编程入门(1)TCP/IP协议族
问题聚焦: 简单地梳理一下TCP/IP各层的功能和常用协议 详细了解ARP(数据链路层)和DNS(应用层)协议的工作原理 1 TCP/IP协议族体系结构 数据链路层: 职责:实现网卡接口的网络 ...
- shell命令批量杀死MySQL连接进程
(1)将全部的MySQL连接进程杀掉 for i in `mysql -uroot -pzhangyun -Bse "show processlist" | grep -v &qu ...
- android学习---SeekBar和RatingBar
SeekBar 拖动条:拖动条和滚动栏类似,当是拖动条能够拖动滑块改变进度 RatingBar 星级评分条:星级评分条与拖动条相似 SeekBar特有的xml属性 android:thumb 指 ...
- 局部敏感哈希-Locality Sensitive Hashing
局部敏感哈希 转载请注明http://blog.csdn.net/stdcoutzyx/article/details/44456679 在检索技术中,索引一直须要研究的核心技术.当下,索引技术主要分 ...
- 远程centos改动yum源
yum -y install unzip发现运行不了,说是找不到unzip的包,搜索发现时由于yum源的问题,那我就改动yum吧, 在网上找到的方法是这么说的: 1. cd /etc/yum.repo ...
- WPF换肤之七:异步
原文:WPF换肤之七:异步 在WinForm时代,相信大家都遇到过这种情形,如果在程序设计过程中遇到了耗时的操作,不使用异步会导致程序假死.当然,在WPF中,这种情况也是存在的,所以我们就需要寻找一种 ...
- SQLiteDatabase和Contentprovider
SQLiteDatabase和Contentprovider这两个数据库,我一般是用前面一个,喜欢它的操作数据库的语句,简单明了,可惜有时遇到数据库同步的问题,有时我们需要在一个数据库下建立多个表,多 ...
- Automatically generate serial number in abap
流水号的产生: 在ABAP开发中,很多地方都需要按照自己的规则生成流水号,把这些流水号保存进透明表,作为唯一标示. 一. 系统中设定流水号 使用T-Code SNRO来创建一个流水号标识对象. 输 ...