使用内核链表实现的一个有头的循环链表,并使用其编写一个冒泡排序
来源:互联网 发布:淘宝客服一般提成多少 编辑:程序博客网 时间:2024/05/16 02:10
include.h文件里都为常用的库:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdbool.h>#include <ctype.h>
macro.h中为常用的宏:
#pragma once#define offsetof(type,member)\((int)(&((type*)0)->member))#define container_of(ptr,type,member)\((type*)((char*)ptr-offsetof(type,member)))
hlist.h中为内核链表的定义:
#pragma once#include "macro.h"struct list_head{struct list_head *prev,*next;};#define INIT_LIST_HEAD(head)\struct list_head head={&head,&head}static inline void LIST_HEAD_INIT(struct list_head *node){node->prev=node;node->next=node;}/* if list is empty ,return 1 */static inline int _is_list_empty(struct list_head *head){return ((head->next==head)&&(head->prev==head));}static inline void _add_list_head(struct list_head *new,struct list_head *prev,struct list_head *next){prev->next=new;new->prev=prev;next->prev=new;new->next=next;}static inline void add_list_from_head(struct list_head *new,struct list_head *head){_add_list_head(new,head,head->next);}static inline void add_list_from_tail(struct list_head *new,struct list_head *head){_add_list_head(new,head->prev,head);}static inline void _delete_list(struct list_head *prev,struct list_head *next){prev->next=next;next->prev=prev;}static inline void delete_list_head(struct list_head *node){_delete_list(node->prev,node->next);LIST_HEAD_INIT(node);}#define for_each(cur,head)\for(cur=(head)->next;cur!=head;cur=(cur)->next)#define for_each_safe(cur,tmp,head)\for(cur=(head)->next,tmp=(cur)->next;\cur!=(head);\cur=tmp,tmp=(tmp)->next)
list.h中为有头的循环链表的接口:
#pragma once#include "hlist.h"#include "include.h"struct list_node{struct list_head list;char data[];};struct list_info{struct list_head *head;void (*add)(struct list_info *info,const void *data_entry,size_t size);void (*add_tail)(struct list_info *info,const void *data_entry,size_t size);void (*del)(struct list_node *node);int (*is_empty)(struct list_info *info);struct list_node *(*search)(struct list_info *info,const void *data_entry,size_t size);};void init_list(struct list_info *info);void destroy_list(struct list_info *info);
list.c为有头循环链表的实现:
#include "list.h"static struct list_node *search_list(struct list_info *info,const void *data_entry,size_t size){struct list_head *cur,*tmp;struct list_node *p;for_each_safe(cur,tmp,info->head){p=container_of(cur,struct list_node,list);if(!memcmp(p->data,data_entry,size))break;}return p;}/* if list is empty , return 1 */static int is_empty(struct list_info *info){return _is_list_empty(info->head);}static void add_list_node_head(struct list_info *info,const void *data_entry,size_t size){struct list_node *node=(struct list_node *)malloc(sizeof(struct list_node)+size);LIST_HEAD_INIT(&node->list);memcpy(node->data,data_entry,size);add_list_from_head(&node->list,info->head);}static void add_list_node_tail(struct list_info *info,const void *data_entry,size_t size){struct list_node *node=(struct list_node *)malloc(sizeof(struct list_node)+size);LIST_HEAD_INIT(&node->list);memcpy(node->data,data_entry,size);add_list_from_tail(&node->list,info->head);}/* delete the node according to the content */static void del_list_node(struct list_node *node){delete_list_head(&node->list);free(node);}void init_list(struct list_info *info){info->head=(struct list_head *)malloc(sizeof(struct list_head));LIST_HEAD_INIT(info->head);info->add=add_list_node_head;info->add_tail=add_list_node_tail;info->del=del_list_node;info->is_empty=is_empty;info->search=search_list;}void destroy_list(struct list_info *info){struct list_node *p;while(!info->is_empty(info)){p=container_of(info->head->next,struct list_node,list);info->del(p);}free(info->head);}
test.c中为使用循环链表实现的一个冒泡排序:
#include "include.h"#include "list.h"#define LIST_LEN 10int cmp(const void *p1 , const void *p2, unsigned long size){return (*((int*)p1) - *((int*)p2));}void pop_sort(struct list_info *info, unsigned long size,int(*cmp)(const void * , const void *, unsigned long)){struct list_head *cur,*tmp;struct list_node *n;struct list_node *p ;char *d = (char *)malloc(size); unsigned long flag = 0;p = container_of(info->head->next, struct list_node, list);for_each_safe(cur, tmp, info->head){p = container_of(cur, struct list_node, list);for(; cur!=info->head ; cur=cur->next){n = container_of(cur , struct list_node , list);if( cmp(p->data, n->data, size)< 0 ){p = n;flag = 1;}}if(flag){memcpy(d, p->data, size);memcpy(p->data, container_of(tmp->prev, struct list_node ,list)->data, size);memcpy(container_of(tmp->prev, struct list_node ,list)->data, d, size);flag = 0;}}}int main(){struct list_info info;struct list_head *cur;struct list_node *n;init_list(&info);int arr[LIST_LEN]={2,44,7,45,77,56,234,9,8,0};int i ;for(i = 0 ; i<LIST_LEN ; ++i){info.add(&info, &arr[i], sizeof(int));}for_each(cur ,info.head){n = container_of(cur , struct list_node , list);printf("%d ", *((int*)n->data));}pop_sort(&info, sizeof(int), cmp);printf("\n");for_each(cur ,info.head){n = container_of(cur , struct list_node , list);printf("%d ", *((int*)n->data));}destroy_list(&info);return 0;}
- 使用内核链表实现的一个有头的循环链表,并使用其编写一个冒泡排序
- 双向循环链表的冒泡排序
- 使用回调函数实现一个通用的冒泡排序,可以排序不同的数据类型。
- 循环链表实现的一个程序
- 循环链表的一个C++实现
- 一个有无限层间接引用的循环链表
- 用C++编写一个使用嵌套循环的程序
- 一个泛型冒泡排序的实现
- 冒泡排序并统计其循环次数
- 编写算法实现建立一个带头结点的含n个元素的双向循环链表H,并在链表H中的第i个位置插入一个元素e
- 使用顺序表实现一个循环队列
- 【C语言】使用回调函数实现一个通用的冒泡排序,可以排序不同的数据类型。
- 使用有头结点的链表进行逆置
- c++实现vector类,并有一个排序和一个查找的泛型算法
- 使用链表的方法进行冒泡排序
- 一个简单的双向循环链表的实现
- 创建一个简单的单向链表并将其倒序输出
- C/C++ | 26-17 已知一个单向链表的头,删除其某一个结点的算法
- 你必须非常努力,才能看起来毫不费力
- Android monkey test 脚本的编写
- UML9种图——类图
- Android TimePicker 不能响应直接输入的问题
- 条码打印机的常用种类
- 使用内核链表实现的一个有头的循环链表,并使用其编写一个冒泡排序
- [DWR(Ajax)]DWR使用笔记
- POJ 1201 SPFA 差分约束
- DWR 入门与应用(一)
- 0.VC(ui)-WM_NCCALCSIZE计算客户区
- CoreData学习笔记(一)
- DWR实例
- Android推荐学习路线图
- SQL Server2000 安装:以前的某个程序安装已在安装计算机上创建挂起的文件操作