📄 doublelist.c
字号:
#include "string.h"
#include "memory.h"
#include "assert.h"
typedef struct LIST_NODE_TAG
{
int key;
struct LIST_NODE_TAG * pre;
struct LIST_NODE_TAG *next;
}LIST_NODE_STRU;
#define LIST_SIZE 10
#define NULL_PTR 0
/*链表头的前趋和后继都是头节点本身*/
#define LIST_HEAD_INIT (name) \
LIST_NODE_STRU name = { 0xffffffff,&(name), &(name) }
/*遍历链表*/
#define list_traver_each(head, pos) \
for (pos = head->next; pos != head; pos = pos->next)
/*加入新节点*/
void __list_add(LIST_NODE_STRU * node, LIST_NODE_STRU * pre, LIST_NODE_STRU * next);
/*删除节点*/
void __list_delete(LIST_NODE_STRU * pre, LIST_NODE_STRU * next);
/*在头接点后加入节点*/
void _list_add_after_head(LIST_NODE_STRU * head, LIST_NODE_STRU * node);
void __list_add(LIST_NODE_STRU * node, LIST_NODE_STRU * pre, LIST_NODE_STRU * next)
{
pre->next = node;
node->pre = pre;
node->next = next;
next->pre = node;
}
void __list_delete(LIST_NODE_STRU * pre, LIST_NODE_STRU * next)
{
pre->next = next;
next->pre = pre;
}
void _list_add_after_head(LIST_NODE_STRU * head, LIST_NODE_STRU * node)
{
/*如果链表为空将加入的第一个元素作为尾部节点*/
if (_list_empty(head))
{
head->pre =node ;
node->next = head;
}
__list_add(node, head, head->next);
return;
}
void _list_delete_ex(LIST_NODE_STRU * head, LIST_NODE_STRU * node)
{
/*如果删除的是尾部节点*/
if (node->next == head)
{
head->pre = node->pre;
node->pre->next = head;
}
/*中间节点*/
else
{
__list_delete(node->pre, node->next);
}
free(node);
return;
}
int _list_empty(LIST_NODE_STRU * head)
{
return head->next == head;
}
int main(void)
{
int i;
LIST_NODE_STRU * node = NULL_PTR;
struct LIST_NODE_TAG * pos = NULL_PTR;
struct LIST_NODE_TAG header;
header.pre = &header;
header.next = &header;
for (i = 0; i < LIST_SIZE; i++)
{
node = (LIST_NODE_STRU*)malloc(sizeof(LIST_NODE_STRU));
assert(node);
node->key = i;
printf("-%d",i);
_list_add_after_head(&header, node);
}
printf("\r\n>>\r\n");
list_traver_each((&header), pos)
{
printf("-%d", pos->key);
}
list_traver_each((&header), pos)
{
_list_delete_ex(&header, pos);
}
if (!_list_empty(&header))
{
printf("\r\n some error");
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -