C语言实现双向循环链表
#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语言实现双向循环链表的更多相关文章
- C语言通用双向循环链表操作函数集
说明 相比Linux内核链表宿主结构可有多个链表结构的优点,本函数集侧重封装性和易用性,而灵活性和效率有所降低. 可基于该函数集方便地构造栈或队列集. 本函数集暂未考虑并发保护. 一 ...
- c语言实现--双向循环链表操作
1,双向链表相当于两个单向循环链表. 2,双向链表的结点定义. 1 struct DULNode 2 { 3 int data; 4 struct DULNode * prior; 5 struct ...
- 双向循环链表(C语言描述)(四)
下面以一个电子英汉词典程序(以下简称电子词典)为例,应用双向循环链表.分离数据结构,可以使逻辑代码独立于数据结构操作代码,程序结构更清晰,代码更简洁:电子词典的增.删.查.改操作分别对应于链表的插入. ...
- 一种神奇的双向循环链表C语言实现
最近在看ucore操作系统的实验指导.里面提要一个双向循环链表的数据结构,挺有意思的. 其实这个数据结构本身并不复杂.在普通链表的基础上加一个前向指针,我们就得到了双向链表,再把头尾节点连起来就是双向 ...
- 1.Go语言copy函数、sort排序、双向链表、list操作和双向循环链表
1.1.copy函数 通过copy函数可以把一个切片内容复制到另一个切片中 (1)把长切片拷贝到短切片中 package main import "fmt" func main() ...
- 【C语言教程】“双向循环链表”学习总结和C语言代码实现!
双向循环链表 定义 双向循环链表和它名字的表意一样,就是把双向链表的两头连接,使其成为了一个环状链表.只需要将表中最后一个节点的next指针指向头节点,头节点的prior指针指向尾节点,链表就能成环儿 ...
- c语言编程之双向循环链表
双向循环链表就是形成两个环,注意每个环的首尾相连基本就可以了. 程序中采用尾插法进行添加节点. #include<stdio.h> #include<stdlib.h> #de ...
- 双向循环链表(C语言描述)(一)
双向循环链表是链表的一种,它的每个节点也包含数据域和指针域.为了方便程序维护,可以单独为数据域定义一种数据类型,这里以整型为例: typedef int LinkedListData; 双向循环链表( ...
- 双向循环链表涉及双向指针的基本操作(C语言)
链表大概分为有无头指针,有无尾指针,是否循环,单向还是双向, 这些都很简单,前提是你要把指针和单链表理解透彻.这些都是基于单链表 的变形,要根据实际问题,选择链表的类型. 头指针的指针域储存着储存头节 ...
随机推荐
- asp.net 分页-自己写分页控件
去年就发表过asp.net 分页-利用后台直接生成html分页 ,那种方法只是单纯的实现了分页,基本不能使用,那时就想写个自己的分页控件,无奈能力有限.最近有点时间了,就自己做出了这个分页控件.我承认 ...
- 【转】Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If
转载地址:http://fanshuyao.iteye.com/blog/1695482 在eclipse启动tomcat时遇到超时45秒的问题: Server Tomcat v7.0 Server ...
- apache和tomcat有什么不同,为什么要整合apache 和tomcat?
1. Apache是web服务器,Tomcat是应用(java)服务器,它只是一个servlet容器,是Apache的扩展.2. Apache和Tomcat都可以做为独立的web服务器来运行,但是Ap ...
- jquery 新闻滚动效果
$(function () { var scrollTimer = null; var delay = 2000; //1.鼠标对新闻触发mouseout事件后每2s调用scr ...
- zigbee学习之路(七):定时器3(中断方式)
一.前言 上次我们学习了了用定时器3进行查询方式来进行溢出判断,今天我们来换一种方式,用中断方式来检测和查询定时器3的溢出. 二.原理与分析 要使用定时器3,我们必须先要配置的是T3CTL,来把定时器 ...
- sublime 使用技巧
使用sublime使遇到的问题: 1.左侧菜单栏隐藏恢复:View ->Side Bar ->Show Side Bar 2.顶部菜单栏隐藏恢复:按住ctrl+shift+p,出现一个框, ...
- 【前端】String.prototype.match() 用法详解
var str="1 plus 2 equal 3" // 正则表达式 console.log(str.match(/\d+/g)); // ["1", &qu ...
- 让下拉框中同时显示Key与Value
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- Java 中的 static 使用之静态方法
与静态变量一样,我们也可以使用 static 修饰方法,称为静态方法或类方法.其实之前我们一直写的 main 方法就是静态方法.静态方法的使用如: 运行结果: 需要注意: 1. 静态方法中可以直接调用 ...
- python_way ,day26 django_admin 自定义
1.想在admin中增加新的字段如图: 默认django只显示 def __str__(self)里面的return的值 from django.contrib import admin # Regi ...