使用列表

我认为最好的方式,成为熟悉的核心列表功能是看一些简单的例子,素材去更好的理解链表。

以下是一个样例。包括创建。加入。删除和遍历链表。

<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核心名单(两)的更多相关文章

  1. Linux中的两种守护进程stand alone和xinetd

    Linux中的两种守护进程stand alone和xinetd --http://www.cnblogs.com/itech/archive/2010/12/27/1914846.html#top 一 ...

  2. linux核心版本号的说明

    日志不会很长,因为每天都在学习,我认为的重点,我自己做的记录,我很高兴能分享给大家: Linux的核心版本编号有点类似如下癿样子: 2.6.18-92.el5 主版本.次版本.释出版本-修改版本 因为 ...

  3. 动态替换Linux核心函数的原理和实现

    转载:https://www.ibm.com/developerworks/cn/linux/l-knldebug/ 动态替换Linux核心函数的原理和实现 在调试Linux核心模块时,有时需要能够实 ...

  4. bootparam - 介绍Linux核心的启动参数

    描叙 Linux 核心在启动的时候可以接受指定的"命令行参数"或"启动参数".在通常情况下,由于核心有可能无法识别某些硬件,或可能将某些硬件识别为不正确的配置, ...

  5. Linux共享库两种加载方式简述

      Linux共享库两种加载方式简述  动态库技术通常能减少程序的大小,节省空间,提高效率,具有很高的灵活性,对于升级软件版本也更加容易.与静态库不同,动态库里面的函数不是执行程序本身 的一部分,而是 ...

  6. Linux核心命令

    Linux核心命令 strace(查看系统调用的一个过程) 例:strace cat /test.txt netstat perf top pidstat mpstat dstat vmstat sl ...

  7. Linux 服务管理两种方式service和systemctl

    Linux 服务管理两种方式service和systemctl 1.service命令 service命令其实是去/etc/init.d目录下,去执行相关程序 # service命令启动redis脚本 ...

  8. 为什么空格拷贝到linux 会变成两个

    为什么空格拷贝到linux 会变成两个 学习了:https://zhidao.baidu.com/question/266438357.html 在vi界面内输入:set paste 然后进行拷贝: ...

  9. Linux 下启动两个tomcat

    Linux 下启动两个tomcat 闲来无事学习nginx,想要配置个load balance.可是先决条件是:得有两个web容器.两个电脑是不用想了.只能想办法在一个机器上启动两个tomcat.原以 ...

随机推荐

  1. Django写的投票系统3(转)

    Django的管理面板默认是不开启的,所以我们需要进行一些设置工作1.在INSTALLED_APPS里面把 django.contrib.admin 前面的注释去掉2.运行 python manage ...

  2. 新西兰gap year_百度百科

    新西兰gap year_百度百科 新西兰gap year    Working Holiday Visa,即打工度假签证.它允许旅行者出于补贴旅行费用的目的而在签证颁发国边打工边旅行.用来鼓励双方国家 ...

  3. 《Linux命令行与shell脚本编程大全》 第十六章 学习笔记

    第十六章:创建函数 基本的脚本函数 创建函数 1.用function关键字,后面跟函数名 function name { commands } 2.函数名后面跟空圆括号,标明正在定义一个函数 name ...

  4. ArcGIS For Flex给定两个

    1.错误叙述性说明 2.错误原因 3.解决方案 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  5. [置顶] 生成学习算法、高斯判别分析、朴素贝叶斯、Laplace平滑——斯坦福ML公开课笔记5

    转载请注明:http://blog.csdn.net/xinzhangyanxiang/article/details/9285001 该系列笔记1-5pdf下载请猛击这里. 本篇博客为斯坦福ML公开 ...

  6. cocos2d-x-3.0新建工程以及移植其他平台

    本文来自:http://www.zaojiahua.com/cocos2d-x-3-0.html 有将近俩个礼拜没有研究cocos2dx了,博主最近刷了些ACM的水题,越做感觉越没意思,这哪是考编程啊 ...

  7. Oracle错误——ORA-03113:在通信信道文件的末尾 解决方案

    起源 今天跟往常一样,登陆PL/SQL,确登陆失败,出现一个错误"ORA-01034"和"ORA-27101"如图: 然后就就通过命令提示符去登陆Oracle, ...

  8. 使用ToggleButton和StackPanel+Border实现圆角开关按钮动画效果

    <ToggleButton Height=" HorizontalAlignment="Left" Margin="138,122,0,0" N ...

  9. Windows Phone 如果你把Pivot控件当成主页面,那么这篇文章你值得看。

    原文:Windows Phone 如果你把Pivot控件当成主页面,那么这篇文章你值得看. 现在很多App都用到了Pivot视图 来当作 整个App主页面.如果你的Pivot视图主页面承载了大量数据的 ...

  10. Test SRM Level Two: CountExpressions, Brute Force

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=8157 这道题目跟扑克牌算24的题目比较像,但要简单一些.点击查 ...