双向链表处理不定长数据的问题
应用方面
在所有的嵌入式设备中,内存有限成了硬件的一个鸡肋,所以, 减少内存的使用 在嵌入式程序中尤为重要。而双向链表就是一种好的选择。
双向链表逻辑处理示意图
数据结构设置
typedef struct doubly_linked_list_s doubly_linked_list_t;
struct doubly_linked_list_s
{
int list_id;
int total_children;
doubly_linked_list_t *children;
doubly_linked_list_t *prev;
doubly_linked_list_t *next;
};
初始化双向链表
typedef struct doubly_linked_list_s doubly_linked_list;
doubly_linked_list *g_Parent = NULL;
doubly_linked_list *init_doubly_linked_list(void)
{
if(NULL != g_Parent)
{
return g_Parent;
}
g_Parent = (doubly_linked_list*)malloc(sizeof(doubly_linked_list));
if(NULL == g_Parent)
{
return NULL;
}
g_Parent->list_id = 0;
g_Parent->total_children = 0;
g_Parent->children = NULL;
g_Parent->prev = g_Parent;
g_Parent->next = g_Parent;
return g_Parent;
}
双向链表节点内存的申请和释放
doubly_linked_list *doubly_linked_palloc(int id)
{
doubly_linked_list* doubly_node = NULL;
doubly_node = (doubly_linked_list*)malloc(sizeof(doubly_linked_list));
if(NULL == doubly_node)
{
return NULL;
}
doubly_node->list_id = id;
doubly_node->children = NULL;
doubly_node->total_children = 0;
doubly_node->next = doubly_node;
doubly_node->prev = doubly_node;
return doubly_node;
}
int doubly_linked_pfree(doubly_linked_list *p)
{
doubly_linked_list *temp = NULL, *cur = NULL;;
if(NULL == p)
{
return 0;
}
if(NULL != p->children)
{
temp = p->children;
while(temp)
{
cur = temp;
free(cur);
cur = NULL;
temp = temp->next;
}
}
p->children = NULL;
p->next = NULL;
p->prev = NULL;
free(p);
p = NULL;
return 0;
}
初始化双向链表数据
doubly_linked_list *init_doubly_linked_list_data(doubly_linked_list* parent)
{
int i = 0;
int cnt = 0;
doubly_linked_list *cur = NULL;
doubly_linked_list *new = NULL;
if(NULL == parent)
{
return NULL;
}
for(i = 0; i < TOTAL_ITEMS; i++)
{
new = doubly_linked_palloc(i);
if(NULL == new)
{
break;
}
if(0 == i)
{
parent->children = new;
cur = g_Parent->children;
}
else
{
new->prev = cur->prev;
new->prev->next = new;
new->next = cur;
cur->prev = new;
}
cnt++;
}
parent->total_children = cnt;
return parent;
}
移除节点,但不释放内存
doubly_linked_list* remove_from_doubly_linked_list(doubly_linked_list* parent,
doubly_linked_list* cur, int direction)
{
if(NULL == parent || NULL == cur)
{
return NULL;
}
if(1 == direction)
{
(cur)->next->prev = (cur)->prev;
(cur)->prev->next = (cur)->next;
parent->children = (cur)->next;
}
else
{
cur->prev->next = cur->next;
cur->next->prev = cur->prev;
}
(cur)->prev = NULL;
(cur)->next = NULL;
return cur;
}
数据在头部/末尾添加
doubly_linked_list* insert_from_doubly_linked_list_tail(doubly_linked_list* parent,
doubly_linked_list* cur)
{
if(NULL == parent || NULL == cur)
{
return NULL;
}
(cur)->prev = (parent)->prev;
(cur)->prev->next = (cur);
(cur)->next = (parent);
(parent)->prev = (cur);
return cur;
}
doubly_linked_list* insert_from_doubly_linked_list_head(doubly_linked_list* parent,
doubly_linked_list* cur)
{
if(NULL == parent || NULL == cur)
{
return NULL;
}
cur->next = parent;
cur->prev = parent->prev;
cur->prev->next = cur;
parent->prev = cur;
return cur;
}
向下/上移动操作的双向链表逻辑
int move_doubly_linked_list_down(doubly_linked_list* parent)
{
doubly_linked_list* cur = NULL;
cur = remove_from_doubly_linked_list(parent, parent->children, DOWN_DIRE);
if(NULL == cur)
{
return -1;
}
cur->list_id = parent->children->prev->list_id+1;
insert_from_doubly_linked_list_tail(parent->children, cur);
print_doubly_linked_list_data(parent);
return 0;
}
int move_doubly_linked_list_up(doubly_linked_list* parent)
{
doubly_linked_list* cur = NULL;
cur = remove_from_doubly_linked_list(parent, parent->children->prev, UP_DIRE);
if(NULL == cur)
{
return -1;
}
cur->list_id = parent->children->list_id-1;
insert_from_doubly_linked_list_head(parent->children, cur);
parent->children = cur;
print_doubly_linked_list_data(parent);
return 0;
}
完整代码
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define UP_DIRE 0 // Move up flag
#define DOWN_DIRE 1 // Move down flag
#define TOTAL_ITEMS 10 // Maximum number of rows displayed.
typedef struct doubly_linked_list_s doubly_linked_list_t;
struct doubly_linked_list_s
{
int list_id;
int total_children;
doubly_linked_list_t *children;
doubly_linked_list_t *prev;
doubly_linked_list_t *next;
};
typedef struct doubly_linked_list_s doubly_linked_list;
doubly_linked_list *g_Parent = NULL;
doubly_linked_list *init_doubly_linked_list(void)
{
if(NULL != g_Parent)
{
return g_Parent;
}
g_Parent = (doubly_linked_list*)malloc(sizeof(doubly_linked_list));
if(NULL == g_Parent)
{
return NULL;
}
g_Parent->list_id = 0;
g_Parent->total_children = 0;
g_Parent->children = NULL;
g_Parent->prev = g_Parent;
g_Parent->next = g_Parent;
return g_Parent;
}
doubly_linked_list *doubly_linked_palloc(int id)
{
doubly_linked_list* doubly_node = NULL;
doubly_node = (doubly_linked_list*)malloc(sizeof(doubly_linked_list));
if(NULL == doubly_node)
{
return NULL;
}
doubly_node->list_id = id;
doubly_node->children = NULL;
doubly_node->total_children = 0;
doubly_node->next = doubly_node;
doubly_node->prev = doubly_node;
return doubly_node;
}
int doubly_linked_pfree(doubly_linked_list *p)
{
doubly_linked_list *temp = NULL, *cur = NULL;;
if(NULL == p)
{
return 0;
}
if(NULL != p->children)
{
temp = p->children;
while(temp)
{
cur = temp;
free(cur);
cur = NULL;
temp = temp->next;
}
}
p->children = NULL;
p->next = NULL;
p->prev = NULL;
free(p);
p = NULL;
return 0;
}
doubly_linked_list *init_doubly_linked_list_data(doubly_linked_list* parent)
{
int i = 0;
int cnt = 0;
doubly_linked_list *cur = NULL;
doubly_linked_list *new = NULL;
if(NULL == parent)
{
return NULL;
}
for(i = 0; i < TOTAL_ITEMS; i++)
{
new = doubly_linked_palloc(i);
if(NULL == new)
{
break;
}
if(0 == i)
{
parent->children = new;
cur = g_Parent->children;
}
else
{
new->prev = cur->prev;
new->prev->next = new;
new->next = cur;
cur->prev = new;
}
cnt++;
}
parent->total_children = cnt;
return parent;
}
int print_doubly_linked_list_data(doubly_linked_list* parent)
{
int i = 0;
doubly_linked_list *cur = NULL;
if(NULL == parent)
{
return -1;
}
cur = g_Parent->children;
for(i = 0; i < parent->total_children && cur; i++)
{
printf("id = %d\n", cur->list_id);
cur = cur->next;
}
printf("\n====================================\n");
return 0;
}
doubly_linked_list* remove_from_doubly_linked_list(doubly_linked_list* parent,
doubly_linked_list* cur, int direction)
{
if(NULL == parent || NULL == cur)
{
return NULL;
}
if(1 == direction)
{
(cur)->next->prev = (cur)->prev;
(cur)->prev->next = (cur)->next;
parent->children = (cur)->next;
}
else
{
cur->prev->next = cur->next;
cur->next->prev = cur->prev;
}
(cur)->prev = NULL;
(cur)->next = NULL;
return cur;
}
doubly_linked_list* insert_from_doubly_linked_list_tail(doubly_linked_list* parent,
doubly_linked_list* cur)
{
if(NULL == parent || NULL == cur)
{
return NULL;
}
(cur)->prev = (parent)->prev;
(cur)->prev->next = (cur);
(cur)->next = (parent);
(parent)->prev = (cur);
return cur;
}
doubly_linked_list* insert_from_doubly_linked_list_head(doubly_linked_list* parent,
doubly_linked_list* cur)
{
if(NULL == parent || NULL == cur)
{
return NULL;
}
cur->next = parent;
cur->prev = parent->prev;
cur->prev->next = cur;
parent->prev = cur;
return cur;
}
int move_doubly_linked_list_down(doubly_linked_list* parent)
{
doubly_linked_list* cur = NULL;
cur = remove_from_doubly_linked_list(parent, parent->children, DOWN_DIRE);
if(NULL == cur)
{
return -1;
}
cur->list_id = parent->children->prev->list_id+1;
insert_from_doubly_linked_list_tail(parent->children, cur);
print_doubly_linked_list_data(parent);
return 0;
}
int move_doubly_linked_list_up(doubly_linked_list* parent)
{
doubly_linked_list* cur = NULL;
cur = remove_from_doubly_linked_list(parent, parent->children->prev, UP_DIRE);
if(NULL == cur)
{
return -1;
}
cur->list_id = parent->children->list_id-1;
insert_from_doubly_linked_list_head(parent->children, cur);
parent->children = cur;
print_doubly_linked_list_data(parent);
return 0;
}
int main(int argc, char const *argv[])
{
init_doubly_linked_list();
init_doubly_linked_list_data(g_Parent);
print_doubly_linked_list_data(g_Parent);
move_doubly_linked_list_down(g_Parent);
move_doubly_linked_list_down(g_Parent);
move_doubly_linked_list_up(g_Parent);
move_doubly_linked_list_up(g_Parent);
return 0;
}
总结
使用双向链表的特性之一,就是可以快速定位到末尾指针,这样可以在向下操作时,快速定位。
定长显示,只需要把第一个或者最后一个进行剔出,不需要释放内存,直接对该节点进行赋值即可,并修改双向链表的处理。