#include <stdio.h>
#include <stdlib.h>
#include <string.h> struct list_head {
struct list_head *next, *prev;
}; #define list_entry(ptr, type, member) \
(type *)( (char *)ptr - ((size_t) &((type *)0)->member)) #define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next) #define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next) static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
} static inline void list_add_tail(struct list_head *new_node, struct list_head *head)
{
new_node->next = head;
new_node->prev = head->prev;
head->prev->next = new_node;
head->prev = new_node;
} static inline void list_del(struct list_head *entry)
{
entry->next->prev = entry->prev;
entry->prev->next = entry->next;
entry->next = NULL;
entry->prev = NULL;
} static inline int list_empty(const struct list_head *head)
{
return head->next == head;
} //使用例子
struct stu {
int num;
char name[20];
struct list_head list;
}; int main(void)
{
struct stu *list_node = NULL;
struct list_head *pos = NULL,*n = NULL;
struct stu *pnode = NULL; struct stu *head = (struct stu *)malloc(sizeof(struct stu));
if (head == NULL) {
printf("file,%s line,%d:malloc error!\n",__FILE__,__LINE__);
exit(1);
} INIT_LIST_HEAD(&head->list); list_node = (struct stu *)malloc(sizeof(struct stu));
if (list_node == NULL) {
printf("file,%s line,%d:malloc error!\n",__FILE__,__LINE__);
exit(1);
} list_node->num = 0;
strcpy(list_node->name,"xiaoming");
list_add_tail(&list_node->list,&head->list); list_node = (struct stu *)malloc(sizeof(struct stu));
if (list_node == NULL) {
printf("file,%s line,%d:malloc error!\n",__FILE__,__LINE__);
exit(1);
} list_node->num = 1;
strcpy(list_node->name,"xiaohua");
list_add_tail(&list_node->list,&head->list); if (list_empty(&head->list)) {
printf("list is empty!\n");
} else {
list_for_each(pos,&head->list) {
pnode = list_entry(pos,struct stu,list);
printf("num:%d,name %s\n",pnode->num,pnode->name);
}
} list_for_each_safe(pos,n,&head->list) {
list_del(pos);
pnode = list_entry(pos,struct stu,list);
printf("num %d has removed from the list!\n",pnode->num);
} free(pnode); free(head);
return 0;
}

  

C语言实现双向循环链表的更多相关文章

  1. C语言通用双向循环链表操作函数集

    说明 相比Linux内核链表宿主结构可有多个链表结构的优点,本函数集侧重封装性和易用性,而灵活性和效率有所降低.     可基于该函数集方便地构造栈或队列集.     本函数集暂未考虑并发保护. 一  ...

  2. c语言实现--双向循环链表操作

    1,双向链表相当于两个单向循环链表. 2,双向链表的结点定义. 1 struct DULNode 2 { 3 int data; 4 struct DULNode * prior; 5 struct ...

  3. 双向循环链表(C语言描述)(四)

    下面以一个电子英汉词典程序(以下简称电子词典)为例,应用双向循环链表.分离数据结构,可以使逻辑代码独立于数据结构操作代码,程序结构更清晰,代码更简洁:电子词典的增.删.查.改操作分别对应于链表的插入. ...

  4. 一种神奇的双向循环链表C语言实现

    最近在看ucore操作系统的实验指导.里面提要一个双向循环链表的数据结构,挺有意思的. 其实这个数据结构本身并不复杂.在普通链表的基础上加一个前向指针,我们就得到了双向链表,再把头尾节点连起来就是双向 ...

  5. 1.Go语言copy函数、sort排序、双向链表、list操作和双向循环链表

    1.1.copy函数 通过copy函数可以把一个切片内容复制到另一个切片中 (1)把长切片拷贝到短切片中 package main import "fmt" func main() ...

  6. 【C语言教程】“双向循环链表”学习总结和C语言代码实现!

    双向循环链表 定义 双向循环链表和它名字的表意一样,就是把双向链表的两头连接,使其成为了一个环状链表.只需要将表中最后一个节点的next指针指向头节点,头节点的prior指针指向尾节点,链表就能成环儿 ...

  7. c语言编程之双向循环链表

    双向循环链表就是形成两个环,注意每个环的首尾相连基本就可以了. 程序中采用尾插法进行添加节点. #include<stdio.h> #include<stdlib.h> #de ...

  8. 双向循环链表(C语言描述)(一)

    双向循环链表是链表的一种,它的每个节点也包含数据域和指针域.为了方便程序维护,可以单独为数据域定义一种数据类型,这里以整型为例: typedef int LinkedListData; 双向循环链表( ...

  9. 双向循环链表涉及双向指针的基本操作(C语言)

    链表大概分为有无头指针,有无尾指针,是否循环,单向还是双向, 这些都很简单,前提是你要把指针和单链表理解透彻.这些都是基于单链表 的变形,要根据实际问题,选择链表的类型. 头指针的指针域储存着储存头节 ...

随机推荐

  1. 关于ajax伪实时动态下拉显示最新数据

    var list= data.data.list; //list是ajax返回的数组Array].Id).length>) { return false; } //$("#learnL ...

  2. 前端工程师的PS默认工作区

    右侧依次是信息.图层.历史记录,如下图:

  3. vs2010 vc++ 统一修改所有工程的目录配置

    vs2005和vs2008中都是通过 工具-选项-项目和解决方案-VC++目录,设置 头文件include .库文件lib.可执行文件dll的路径,以便在引用dll动态链接库文件时,可以查找到该文件的 ...

  4. IndexOutOfBoundsException ArrayList 访问越界

    java.lang.IndexOutOfBoundsException: Index: 3, Size: 2

  5. Oracle系统表整理+常用SQL语句收集

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  6. selenium eclipse环境搭建

    1.python 3.5下载及安装 2.setuptools 与pip 下载地址是:http://pypi.Python.org/pypi/setuptools http://pypi.Python. ...

  7. 课时8—弹窗modal

    首先弹窗的实现效果如下: 主要实现的代码如下: CSS: .header,.footer,.wrap-page{ position:absolute; left:; right:; backgroun ...

  8. Zepto与jQuery的区别

    1. 事件绑定 var isTouch= !!navigator.userAgent.match(/AppleWebKit.*Mobile.*/), eStart = isTouch ? 'touch ...

  9. wifidog 配置中文说明

    #网关IDGatewayID default#外部网卡ExternalInterface eth0#无线网卡GatewayInterface eth0#无线IPGatewayAddress 192.1 ...

  10. poj 2763 Housewife Wind

    题目链接 分析:这道题是树链剖分的裸题,把边的信息保存在深度大的那个节点上就行了. 一开始写的邻接表,居然TLE了.后来百度发现有人说前向星跑得比较快?我不是很明白,但是改成前向星以后的确快了很多,邻 ...