双向链表

  • 代码来自RThread中的rtservice.h文件

rt_inline void rt_list_init(rt_list_t *l)

{
  l->next = l->prev = l;
}

/**
* @brief insert a node after a list
*
* @param l list to insert it
* @param n new node to be inserted
*/
rt_inline void rt_list_insert_after(rt_list_t *l, rt_list_t *n)
{
  l->next->prev = n;
  n->next = l->next;

  l->next = n;
  n->prev = l;
}

/**
* @brief insert a node before a list
*
* @param n new node to be inserted
* @param l list to insert it
*/
rt_inline void rt_list_insert_before(rt_list_t *l, rt_list_t *n)
{
  l->prev->next = n;
  n->prev = l->prev;

  l->prev = n;
  n->next = l;
}

/**
* @brief remove node from list.
* @param n the node to remove from the list.
*/
rt_inline void rt_list_remove(rt_list_t *n)
{
  n->next->prev = n->prev;
  n->prev->next = n->next;

  n->next = n->prev = n;
}

/**
* @brief tests whether a list is empty
* @param l the list to test.
*/
rt_inline int rt_list_isempty(const rt_list_t *l)
{
  return l->next == l;
}

/**
* @brief get the list length
* @param l the list to get.
*/
rt_inline unsigned int rt_list_len(const rt_list_t *l)
{
  unsigned int len = 0;
  const rt_list_t *p = l;
  while (p->next != l)
  {
    p = p->next;
    len ++;
  }

  return len;
}

 

                      rt_list_insert_before

                      rt_list_insert_after

 

posted @ 2024-03-17 21:44  北溟有渔  阅读(4)  评论(0编辑  收藏  举报