数据存储,链表还是数组?
作者 | 中移物联OneOS
原文链接:https://my.oschina.net/u/5443273/blog/7782411
引言
链表和数组是两种不同的数据存储方式。链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。数组是把具有相同类型的若干元素按有序的形式组织起来的一种形式,数组中的各元素的存储是有先后顺序的,它们在内存中按照这个先后顺序连续存放在一起。本文将对这两种存储方式的优缺点做一个大致的介绍,并详细介绍链表在操作系统中定义和使用的方式。
一、链表和数组
链表是链式的存储结构,数组是顺序的存储结构,其在内存存储上的不同形式决定了其各自的特点。
链表通过指针来链接元素,链表中的结点顺序关系由指针来体现;数组将元素按次序依次存储,元素顺序关系由元素在数组中的位置(下标)确定。 链表节点的存储单元在程序执行时动态向系统申请,链表的结点个数可按需要增减;数组元素的存储单元在数组定义时分配,其元素个数是固定的,对于不是固定长度的列表,用可能最大长度的数组来描述。 链表插入删除元素不需要移动元素,且较为容易实现长度扩充,但是寻找某个元素较为困难;数组寻找某个元素较为简单,但插入与删除比较复杂。
总体来说,链表使用指针将一系列数据节点链接成数据链,相对于数组,它具有良好的动态性,建立链表时不需要提前知道数据量,可以随时分配空间,可以高效地在链表中的任意位置插入或者删除数据。操作系统中存在着大量的基础数据结构链表和链表项,理解链表对理解操作系统至关重要。
二、单向链表和双向链表
通常链表数据结构包含两部分,一部分是数据域,用于存储数据;另外一部分是指针域,用于建立与其它节点的关系。链表项中可以包含一个指向下一个链表项的指针而不包含指向上一个链表的指针,也可以两者都包含,前者称为单向链表,后者为双向链表。
OneOS 物联网操作系统中提供的链表不包含数据域,使用时不是在链表结构中包含数据,而是在用户的数据结构中包含链表节点,操作系统中提供了双向链表及单向链表的一些比较通用的操作接口。
2.1 单向链表
OneOS 操作系统中的单向链表包含一个节点指针,这个节点指针指向下一个节点。OneOS 操作系统中的单向链表是一个非循环链表,单向链表本身首尾并非相连,单向链表中的最后一个节点指向 OS_NULL(循环链表单向链表中的最后一个节点指向单向链表中的第一个节点),其示意图如下所示。
OneOS 操作系统中单向链表节点的结构体如下所示。
struct os_slist_node
{
struct os_slist_node *next; /* Point to next node */
};
2.2 双向链表
struct os_list_node
{
struct os_list_node *next; /* Point to next node */
struct os_list_node *prev; /* point to previous node */
};
三、应用示例
单向链表应用示例
#include <oneos_config.h>#include <dlog.h>#include <os_errno.h>#include <shell.h>#include <string.h>#include <os_memory.h>#include <os_list.h>
#define TEST_TAG "TEST"
#define STUDENT_NUM 10#define TEST_NAME_MAX 16
char *name[STUDENT_NUM] = {"xiaoming", "xiaohua", "xiaoqiang", "xiaoli", "xiaofang", "zhangsan", "lisi", "wangwu", "zhaoliu", "qianqi"};uint32_t score[STUDENT_NUM] = {70, 83, 68, 80, 88, 86, 78, 92, 55, 82};
struct student_score
{
os_slist_node_t list_node;
char name[TEST_NAME_MAX];
uint32_t id;
uint32_t score;
};typedef struct student_score student_score_t;
void single_list_sample(void)
{
uint32_t i = 0;
os_slist_node_t list_head = OS_SLIST_INIT(list_head);
student_score_t *data;
os_slist_node_t *node_temp;
os_slist_node_t *node;
LOG_W(TEST_TAG, "single_list_sample insert data");
for (i = 0; i < STUDENT_NUM; i++)
{
data = os_malloc(sizeof(student_score_t));
data->id = i;
memset(data->name, 0, TEST_NAME_MAX);
strncpy(data->name, name[i], TEST_NAME_MAX);
data->score = score[i];
if (i < STUDENT_NUM/2)
{
LOG_W(TEST_TAG, "insert tail -- id:%d score:%d name:%s", data->id, data->score, data->name);
os_slist_add_tail(&list_head, &data->list_node);
}
else
{
LOG_W(TEST_TAG, "insert front-- id:%d score:%d name:%s", data->id, data->score, data->name);
os_slist_add(&list_head, &data->list_node);
}
}
LOG_W(TEST_TAG, "single_list_sample show result");
os_slist_for_each(node, &list_head)
{
data = os_slist_entry(node, student_score_t, list_node);
LOG_W(TEST_TAG, "id:%d score:%d name:%s", data->id, data->score, data->name);
}
LOG_W(TEST_TAG, "single_list_sample list_len is:%d", os_slist_len(&list_head));
LOG_W(TEST_TAG, "single_list_sample delete the score less than 60");
os_slist_for_each_safe(node, node_temp, &list_head)
{
data = os_slist_entry(node, student_score_t, list_node);
if (data->score < 60)
{
LOG_W(TEST_TAG, "delete -- id:%d score:%d name:%s", data->id, data->score, data->name);
os_slist_del(&list_head, &data->list_node);
os_free(data);
}
}
LOG_W(TEST_TAG, "single_list_sample list_len is:%d", os_slist_len(&list_head));
LOG_W(TEST_TAG, "single_list_sample show result, and then delete");
os_slist_for_each_safe(node, node_temp, &list_head)
{
data = os_slist_entry(node, student_score_t, list_node);
LOG_W(TEST_TAG, "delete -- id:%d score:%d name:%s", data->id, data->score, data->name);
os_slist_del(&list_head, &data->list_node);
os_free(data);
}
LOG_W(TEST_TAG, "single_list_sample list_len is:%d", os_slist_len(&list_head));
}
SH_CMD_EXPORT(test_single_list, single_list_sample, "test single list");
sh>test_single_list
W/TEST: single_list_sample insert data
W/TEST: insert tail -- id:0 score:70 name:xiaoming
W/TEST: insert tail -- id:1 score:83 name:xiaohua
W/TEST: insert tail -- id:2 score:68 name:xiaoqiang
W/TEST: insert tail -- id:3 score:80 name:xiaoli
W/TEST: insert tail -- id:4 score:88 name:xiaofang
W/TEST: insert front-- id:5 score:86 name:zhangsan
W/TEST: insert front-- id:6 score:78 name:lisi
W/TEST: insert front-- id:7 score:92 name:wangwu
W/TEST: insert front-- id:8 score:55 name:zhaoliu
W/TEST: insert front-- id:9 score:82 name:qianqi
W/TEST: single_list_sample show result
W/TEST: id:9 score:82 name:qianqi
W/TEST: id:8 score:55 name:zhaoliu
W/TEST: id:7 score:92 name:wangwu
W/TEST: id:6 score:78 name:lisi
W/TEST: id:5 score:86 name:zhangsan
W/TEST: id:0 score:70 name:xiaoming
W/TEST: id:1 score:83 name:xiaohua
W/TEST: id:2 score:68 name:xiaoqiang
W/TEST: id:3 score:80 name:xiaoli
W/TEST: id:4 score:88 name:xiaofang
W/TEST: single_list_sample list_len is:10
W/TEST: single_list_sample delete the score less than 60
W/TEST: delete -- id:8 score:55 name:zhaoliu
W/TEST: single_list_sample list_len is:9
W/TEST: single_list_sample show result, and then delete
W/TEST: delete -- id:9 score:82 name:qianqi
W/TEST: delete -- id:7 score:92 name:wangwu
W/TEST: delete -- id:6 score:78 name:lisi
W/TEST: delete -- id:5 score:86 name:zhangsan
W/TEST: delete -- id:0 score:70 name:xiaoming
W/TEST: delete -- id:1 score:83 name:xiaohua
W/TEST: delete -- id:2 score:68 name:xiaoqiang
W/TEST: delete -- id:3 score:80 name:xiaoli
W/TEST: delete -- id:4 score:88 name:xiaofang
W/TEST: single_list_sample list_len is:0
#include <oneos_config.h>#include <dlog.h>#include <os_errno.h>#include <shell.h>#include <string.h>#include <os_memory.h>#include <os_list.h>
#define TEST_TAG "TEST"
#define STUDENT_NUM 10#define TEST_NAME_MAX 16
char *name[STUDENT_NUM] = {"xiaoming", "xiaohua", "xiaoqiang", "xiaoli", "xiaofang", "zhangsan", "lisi", "wangwu", "zhaoliu", "qianqi"};uint32_t score[STUDENT_NUM] = {70, 83, 68, 80, 88, 86, 78, 92, 55, 82};
struct student_score
{
os_list_node_t list_node;
char name[TEST_NAME_MAX];
uint32_t id;
uint32_t score;
};typedef struct student_score student_score_t;
void list_sample(void)
{
uint32_t i = 0;
os_list_node_t list_head = OS_LIST_INIT(list_head);
student_score_t *data;
student_score_t *data_temp;
os_list_node_t *node;
os_list_node_t *node_temp;
LOG_W(TEST_TAG, "list_sample insert data");
for (i = 0; i < STUDENT_NUM; i++)
{
data = os_malloc(sizeof(student_score_t));
data->id = i;
memset(data->name, 0, TEST_NAME_MAX);
strncpy(data->name, name[i], TEST_NAME_MAX);
data->score = score[i];
if (i < STUDENT_NUM/2)
{
LOG_W(TEST_TAG, "insert tail -- id:%d score:%d name:%s", data->id, data->score, data->name);
os_list_add_tail(&list_head, &data->list_node);
}
else
{
LOG_W(TEST_TAG, "insert front-- id:%d score:%d name:%s", data->id, data->score, data->name);
os_list_add(&list_head, &data->list_node);
}
}
LOG_W(TEST_TAG, "list_sample show result");
os_list_for_each_entry(data, &list_head, student_score_t, list_node)
{
LOG_W(TEST_TAG, "id:%d score:%d name:%s", data->id, data->score, data->name);
}
LOG_W(TEST_TAG, "list_sample list_len is:%d", os_list_len(&list_head));
LOG_W(TEST_TAG, "list_sample delete the score less than 60");
os_list_for_each_entry_safe(data, data_temp, &list_head, student_score_t, list_node)
{
if (data->score < 60)
{
LOG_W(TEST_TAG, "delete -- id:%d score:%d name:%s", data->id, data->score, data->name);
os_list_del(&data->list_node);
os_free(data);
}
}
LOG_W(TEST_TAG, "list_sample list_len is:%d", os_list_len(&list_head));
LOG_W(TEST_TAG, "list_sample show result, and then delete");
os_list_for_each_safe(node, node_temp, &list_head)
{
data = os_list_entry(node, student_score_t, list_node);
LOG_W(TEST_TAG, "delete -- id:%d score:%d name:%s", data->id, data->score, data->name);
os_list_del(&data->list_node);
os_free(data);
}
LOG_W(TEST_TAG, "list_sample list_len is:%d", os_list_len(&list_head));
}
SH_CMD_EXPORT(test_list, list_sample, "test list");
sh>test_list
W/TEST: list_sample insert data
W/TEST: insert tail -- id:0 score:70 name:xiaoming
W/TEST: insert tail -- id:1 score:83 name:xiaohua
W/TEST: insert tail -- id:2 score:68 name:xiaoqiang
W/TEST: insert tail -- id:3 score:80 name:xiaoli
W/TEST: insert tail -- id:4 score:88 name:xiaofang
W/TEST: insert front-- id:5 score:86 name:zhangsan
W/TEST: insert front-- id:6 score:78 name:lisi
W/TEST: insert front-- id:7 score:92 name:wangwu
W/TEST: insert front-- id:8 score:55 name:zhaoliu
W/TEST: insert front-- id:9 score:82 name:qianqi
W/TEST: list_sample show result
W/TEST: id:9 score:82 name:qianqi
W/TEST: id:8 score:55 name:zhaoliu
W/TEST: id:7 score:92 name:wangwu
W/TEST: id:6 score:78 name:lisi
W/TEST: id:5 score:86 name:zhangsan
W/TEST: id:0 score:70 name:xiaoming
W/TEST: id:1 score:83 name:xiaohua
W/TEST: id:2 score:68 name:xiaoqiang
W/TEST: id:3 score:80 name:xiaoli
W/TEST: id:4 score:88 name:xiaofang
W/TEST: list_sample list_len is:10
W/TEST: list_sample delete the score less than 60
W/TEST: delete -- id:8 score:55 name:zhaoliu
W/TEST: list_sample list_len is:9
W/TEST: list_sample show result, and then delete
W/TEST: delete -- id:9 score:82 name:qianqi
W/TEST: delete -- id:7 score:92 name:wangwu
W/TEST: delete -- id:6 score:78 name:lisi
W/TEST: delete -- id:5 score:86 name:zhangsan
W/TEST: delete -- id:0 score:70 name:xiaoming
W/TEST: delete -- id:1 score:83 name:xiaohua
W/TEST: delete -- id:2 score:68 name:xiaoqiang
W/TEST: delete -- id:3 score:80 name:xiaoli
W/TEST: delete -- id:4 score:88 name:xiaofang
W/TEST: list_sample list_len is:0
往期推荐
开源方案低成本复现ChatGPT流程,仅需1.6GB显存即可体验
顶流开源项目作者全职做开源的“血泪史”:入狱、耗尽积蓄、被网暴……
质疑我违规窃取开源代码?拉黑你!
这里有最新开源资讯、软件更新、技术干货等内容
点这里 ↓↓↓ 记得 关注✔ 标星⭐ 哦
微信扫码关注该文公众号作者