好用的链表操作库。

1. linklist.c

#include "linklist.h"

/* Allocate new list. */
struct list *
list_new (void)
{
  return (struct list *)calloc(1, sizeof(struct list));
}

/* Free list. */
void
list_free (struct list *l)
{
  free(l);
}

/* Allocate new listnode.  Internal use only. */
static struct listnode *
listnode_new (void)
{
  return (struct listnode *)calloc(1,sizeof (struct listnode));
}

/* Free listnode. */
static void
listnode_free (struct listnode *node)
{
  free(node);
}

/* Add new data to the list. */
void
listnode_add (struct list *list, void *val)
{
  struct listnode *node;

  assert (val != NULL);

  node = listnode_new ();

  node->prev = list->tail;
  node->data = val;

  if (list->head == NULL)
    list->head = node;
  else
    list->tail->next = node;
  list->tail = node;

  list->count++;
}

/*
 * Add a node to the list.  If the list was sorted according to the
 * cmp function, insert a new node with the given val such that the
 * list remains sorted.  The new node is always inserted; there is no
 * notion of omitting duplicates.
 */
void
listnode_add_sort (struct list *list, void *val)
{
  struct listnode *n;
  struct listnode *new;

  assert (val != NULL);

  new = listnode_new ();
  new->data = val;

  if (list->cmp)
    {
      for (n = list->head; n; n = n->next)
	{
	  if ((*list->cmp) (val, n->data) < 0)
	    {
	      new->next = n;
	      new->prev = n->prev;

	      if (n->prev)
		n->prev->next = new;
	      else
		list->head = new;
	      n->prev = new;
	      list->count++;
	      return;
	    }
	}
    }

  new->prev = list->tail;

  if (list->tail)
    list->tail->next = new;
  else
    list->head = new;

  list->tail = new;
  list->count++;
}

void
listnode_add_after (struct list *list, struct listnode *pp, void *val)
{
  struct listnode *nn;

  assert (val != NULL);

  nn = listnode_new ();
  nn->data = val;

  if (pp == NULL)
    {
      if (list->head)
	list->head->prev = nn;
      else
	list->tail = nn;

      nn->next = list->head;
      nn->prev = pp;

      list->head = nn;
    }
  else
    {
      if (pp->next)
	pp->next->prev = nn;
      else
	list->tail = nn;

      nn->next = pp->next;
      nn->prev = pp;

      pp->next = nn;
    }
  list->count++;
}

/* Delete specific date pointer from the list. */
void
listnode_delete (struct list *list, void *val)
{
  struct listnode *node;

  assert(list);
  for (node = list->head; node; node = node->next)
    {
      if (node->data == val)
	{
	  if (node->prev)
	    node->prev->next = node->next;
	  else
	    list->head = node->next;

	  if (node->next)
	    node->next->prev = node->prev;
	  else
	    list->tail = node->prev;

	  list->count--;
	  listnode_free (node);
	  return;
	}
    }
}

/* Return first node's data if it is there.  */
void *
listnode_head (struct list *list)
{
  struct listnode *node;

  assert(list);
  node = list->head;

  if (node)
    return node->data;
  return NULL;
}

/* Delete all listnode from the list. */
void
list_delete_all_node (struct list *list)
{
  struct listnode *node;
  struct listnode *next;

  assert(list);
  for (node = list->head; node; node = next)
    {
      next = node->next;
      if (list->del)
	(*list->del) (node->data);
      listnode_free (node);
    }
  list->head = list->tail = NULL;
  list->count = 0;
}

/* Delete all listnode then free list itself. */
void
list_delete (struct list *list)
{
  assert(list);
  list_delete_all_node (list);
  list_free (list);
}

/* Lookup the node which has given data. */
struct listnode *
listnode_lookup (struct list *list, void *data)
{
  struct listnode *node;

  assert(list);
  for (node = listhead(list); node; node = listnextnode (node))
    if (data == listgetdata (node))
      return node;
  return NULL;
}

/* Delete the node from list.. */
void
list_delete_node (struct list *list, struct listnode *node)
{
  if (node->prev)
    node->prev->next = node->next;
  else
    list->head = node->next;
  if (node->next)
    node->next->prev = node->prev;
  else
    list->tail = node->prev;
  list->count--;
  listnode_free (node);
}

void
list_add_node_prev (struct list *list, struct listnode *current, void *val)
{
  struct listnode *node;

  assert (val != NULL);

  node = listnode_new ();
  node->next = current;
  node->data = val;

  if (current->prev == NULL)
    list->head = node;
  else
    current->prev->next = node;

  node->prev = current->prev;
  current->prev = node;

  list->count++;
}

void
list_add_node_next (struct list *list, struct listnode *current, void *val)
{
  struct listnode *node;

  assert (val != NULL);

  node = listnode_new ();
  node->prev = current;
  node->data = val;

  if (current->next == NULL)
    list->tail = node;
  else
    current->next->prev = node;

  node->next = current->next;
  current->next = node;

  list->count++;
}

void
list_add_list (struct list *l, struct list *m)
{
  struct listnode *n;

  for (n = listhead (m); n; n = listnextnode (n))
    listnode_add (l, n->data);
}

2. linklist.h

#ifndef __LINKLIST_H
#define __LINKLIST_H

/* listnodes must always contain data to be valid. Adding an empty node
 * to a list is invalid
 */
struct listnode
{
  struct listnode *next;
  struct listnode *prev;

  /* private member, use getdata() to retrieve, do not access directly */
  void *data;
};

struct list
{
  struct listnode *head;
  struct listnode *tail;

  /* invariant: count is the number of listnodes in the list */
  unsigned int count;

  /*
   * Returns -1 if val1 < val2, 0 if equal?, 1 if val1 > val2.
   * Used as definition of sorted for listnode_add_sort
   */
  int (*cmp) (void *val1, void *val2);

  /* callback to free user-owned data when listnode is deleted. supplying
   * this callback is very much encouraged!
   */
  void (*del) (void *val);
};

#define listnextnode(X) ((X) ? ((X)->next) : NULL)
#define listhead(X) ((X) ? ((X)->head) : NULL)
#define listtail(X) ((X) ? ((X)->tail) : NULL)
#define listcount(X) ((X)->count)
#define list_isempty(X) ((X)->head == NULL && (X)->tail == NULL)
#define listgetdata(X) (assert((X)->data != NULL), (X)->data)

/* Prototypes. */
extern struct list *list_new(void); /* encouraged: set list.del callback on new lists */
extern void list_free (struct list *);

extern void listnode_add (struct list *, void *);
extern void listnode_add_sort (struct list *, void *);
extern void listnode_add_after (struct list *, struct listnode *, void *);
extern void listnode_delete (struct list *, void *);
extern struct listnode *listnode_lookup (struct list *, void *);
extern void *listnode_head (struct list *);

extern void list_delete (struct list *);
extern void list_delete_all_node (struct list *);

extern void list_delete_node (struct list *, struct listnode *);
extern void list_add_node_prev (struct list *, struct listnode *, void *);
extern void list_add_node_next (struct list *, struct listnode *, void *);
extern void list_add_list (struct list *, struct list *);

/* List iteration macro.
 * Usage: for (ALL_LIST_ELEMENTS (...) { ... }
 * It is safe to delete the listnode using this macro.
 */
#define ALL_LIST_ELEMENTS(list,node,nextnode,data)   (node) = listhead(list), ((data) = NULL);   (node) != NULL &&     ((data) = listgetdata(node),(nextnode) = node->next, 1);   (node) = (nextnode), ((data) = NULL)

/* read-only list iteration macro.
 * Usage: as per ALL_LIST_ELEMENTS, but not safe to delete the listnode Only
 * use this macro when it is *immediately obvious* the listnode is not
 * deleted in the body of the loop. Does not have forward-reference overhead
 * of previous macro.
 */
#define ALL_LIST_ELEMENTS_RO(list,node,data)   (node) = listhead(list), ((data) = NULL);  (node) != NULL && ((data) = listgetdata(node), 1);   (node) = listnextnode(node), ((data) = NULL)

/* these *do not* cleanup list nodes and referenced data, as the functions
 * do - these macros simply {de,at}tach a listnode from/to a list.
 */

/* List node attach macro.  */
#define LISTNODE_ATTACH(L,N)   do {     (N)->prev = (L)->tail;     if ((L)->head == NULL)       (L)->head = (N);     else       (L)->tail->next = (N);     (L)->tail = (N);     (L)->count++;   } while (0)

/* List node detach macro.  */
#define LISTNODE_DETACH(L,N)   do {     if ((N)->prev)       (N)->prev->next = (N)->next;     else       (L)->head = (N)->next;     if ((N)->next)       (N)->next->prev = (N)->prev;     else       (L)->tail = (N)->prev;     (L)->count--;   } while (0)

#endif /* __LINKLIST_H */
时间: 2024-10-21 10:36:34

好用的链表操作库。的相关文章

数据结构上机测试2-1:单链表操作A (顺序建表+关键字删除)

数据结构上机测试2-1:单链表操作A Time Limit: 1000MS Memory limit: 4096K 题目描述 输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据m,将单链表中的值为m的结点全部删除.分别输出建立的初始单链表和完成删除后的单链表. 输入 第一行输入数据个数n: 第二行依次输入n个整数: 第三行输入欲删除数据m. 输出 第一行输出原始单链表的长度: 第二行依次输出原始单链表的数据: 第三行输出完成删除后的单链表长度: 第四行依次输出完成删除后的

zBase --轻量级DOM操作库

项目地址:ZengTianShengZ-github zBase-1.2.0 --v3 修复部分bug,添加AMD规范测试 zBase-1.1.0 --v2 对 v1 版本做了升级,优化DOM查找,简化API,提高代码可读性,支持模块化 支持 AMD & CommonJS zBase-1.0.0 --v1 没有任何依赖 轻量级的Dom操作库,封装一些常用的css选择器和事件操作等 Quick start $ npm install $ gulp scripts 使用npm安装 zBase: $

单链表操作

#include<stdio.h>#include<malloc.h> typedef struct Node{ int data; struct Node *next;}LinkList; //就地反转int LinkListRerverse(LinkList *head){ LinkList *q,*p; p = head->next; head->next = NULL; while(p != NULL){ q = p->next; p->next =

数据结构之链表操作

#include <IOSTREAM> #include <IOMANIP> #include <STRING> using namespace std; typedef struct Student { int id; char name[20]; char sex[20]; struct Student *next; }node; typedef node * pnode; pnode Create(); void Print(pnode h); void Sort

单向链表操作 原文:http://canlynet.blog.163.com/blog/static/255013652009113001041903/

/********************************************** 功能:单向链表操作(注意Head指针 需要被改变时传入的是二级指针) 日期:2009.12.29 作者:DC ***********************************************/ #include <stdio.h> #include <stdlib.h> #define OVER_FLOW -2 #define OK 0 typedef int ElemTy

单链表操作系列

#include<stdio.h> #include<stdlib.h> typedef int ElemType; //定义结点类型 typedef struct Node { ElemType data; struct Node *next; }LNode,*LinkList; //单链表的建立1,头插法建立单链表,逆序生成 LinkList LinkListCreateH() { LinkList L,p; L = (LinkList)malloc(sizeof(LNode)

链表操作 -- 有环链表问题

参考: http://blog.163.com/[email protected]/blog/static/1113522592011828104617420/ 问题: 判断一个链表中是否有环. 分析: 我们都知道,当一个链表中没有环时,我们使用一个指针能从头遍历到尾:当链表中有环时,链表会在环中旋转. 当我们只使用一个链表指针时,可想到方法就是使用额外的数据结构来存储遍历过的每个节点,在遍历next节点时,判断next节点是否已存在于存储的节点中. 存储结构可以选择为hashTable,这样的

C# 链表操作

关于链表操作,在C#当中微软已经提供了一个LinkedList<T>的数据结构,通过这个类提供的一系列方法就能够实现链表操作. 这里我提供一段代码,这是在论坛里面有人提问时给出的代码,它实现了自定义链表的操作(读者可以在此基础上进一步完善).因为这段代码涉及一些C#技巧,所以贴出来给初学者学习C#提供一点参考. 实体类:     /// <summary>     /// 学生类     /// </summary>     public class Student  

2014 年10个最佳的PHP图像操作库

2014 年10个最佳的PHP图像操作库 Thomas Boutell 以及众多的开发者创造了以GD图形库闻名的一个图形软件库,用于动态的图形计算. GD提供了对于诸如C, Perl, Python, PHP, OCaml等等诸多编程语言的支持. 除了生成HTML输出之外, 你还可以使用php以众多的像 PNG, JPEG, GIF, WBMP,以及 XPM这样的图形文件格式来创建和计算图形文件. 并且,php的使用还能让你可以直接向一个浏览器输出图像流. 为了做到这一点,你需要借助于GD库图像