mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-03 23:56:54 +08:00
添加动态数组实现的列表
This commit is contained in:
parent
18d30450cd
commit
496ac36ca8
@ -6,7 +6,8 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
|
@ -23,32 +23,32 @@
|
||||
<ProjectGuid>{3990515A-48AA-4CFF-9014-C35B11F02C87}</ProjectGuid>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<RootNamespace>datastruct</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
|
@ -1,6 +1,8 @@
|
||||
|
||||
#include "list.h"
|
||||
|
||||
|
||||
#ifdef LINK_LIST
|
||||
bool list_init(plist_t *list)
|
||||
{
|
||||
*list = (plist_t)malloc(sizeof(list_t));
|
||||
@ -163,4 +165,166 @@ void list_traversal_reversed(plist_t list, list_data_disp_t disp)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
bool list_init(list_t list)
|
||||
{
|
||||
// list->capacity = 64;
|
||||
list->capacity = 8;
|
||||
|
||||
list->size = 0;
|
||||
list->extend_ratio = 2;
|
||||
list->array = (list_data_t *)malloc(list->capacity * sizeof(list_data_t));
|
||||
if (list->array == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void list_destory(list_t list)
|
||||
{
|
||||
if (list->array != NULL)
|
||||
{
|
||||
free(list->array);
|
||||
}
|
||||
}
|
||||
|
||||
int list_capacity(list_t list)
|
||||
{
|
||||
return list->capacity;
|
||||
}
|
||||
|
||||
int list_size(list_t list)
|
||||
{
|
||||
return list->size;
|
||||
}
|
||||
|
||||
bool list_empty(list_t list)
|
||||
{
|
||||
return list_size(list) == 0 ? true : false;
|
||||
}
|
||||
|
||||
bool list_insert(list_t list, int index, list_data_t data)
|
||||
{
|
||||
assert(index >= 0 && index <= list_size(list));
|
||||
if (list_size(list) == list_capacity(list))
|
||||
{
|
||||
int capacity = list->capacity * list->extend_ratio;
|
||||
list_data_t * array = (list_data_t*)realloc(list->array, capacity * sizeof(list_data_t));
|
||||
if (array == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
list->array = array;
|
||||
list->capacity = capacity;
|
||||
}
|
||||
memmove(&list->array[index + 1], &list->array[index], list_size(list));
|
||||
list->array[index] = data;
|
||||
list->size += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool list_append(list_t list, list_data_t data)
|
||||
{
|
||||
if (list_size(list) == list_capacity(list))
|
||||
{
|
||||
int capacity = list->capacity * list->extend_ratio;
|
||||
list_data_t * array = (list_data_t*)realloc(list->array, capacity * sizeof(list_data_t));
|
||||
if (array == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
list->array = array;
|
||||
list->capacity = capacity;
|
||||
}
|
||||
list->array[list->size] = data;
|
||||
list->size += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
int list_delete(list_t list, int index)
|
||||
{
|
||||
assert(index >= 0 && index < list_size(list));
|
||||
int temp = 0;
|
||||
temp = list->array[index];
|
||||
if (index != list_size(list))
|
||||
{
|
||||
memmove(&list->array[index], &list->array[index + 1], (list_size(list) - 1 - index) * sizeof(list_data_t));
|
||||
//for (int i = index; i < list_size(list) - 1; i++)
|
||||
//{
|
||||
// list->array[i] = list->array[i + 1];
|
||||
//}
|
||||
}
|
||||
list->size -= 1;
|
||||
return temp;
|
||||
}
|
||||
|
||||
bool list_clear(list_t list)
|
||||
{
|
||||
memset(list->array, 0, list->size);
|
||||
list->size = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
list_data_t list_get(list_t list, int index)
|
||||
{
|
||||
assert(index >= 0 && index < list_size(list));
|
||||
return list->array[index];
|
||||
}
|
||||
|
||||
bool list_set(list_t list, int index, list_data_t data)
|
||||
{
|
||||
assert(index >= 0 && index < list_size(list));
|
||||
list->array[index] = data;
|
||||
return true;
|
||||
}
|
||||
|
||||
void list_print(list_t list)
|
||||
{
|
||||
if (!list_empty(list))
|
||||
{
|
||||
for (int i = 0; i < list_size(list); i++)
|
||||
{
|
||||
printf("%d ", list->array[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void list_test(void)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
struct _list list;
|
||||
list_init(&list);
|
||||
|
||||
printf("----- append -----\n");
|
||||
for (i = 0; i < 18; i++)
|
||||
{
|
||||
list_append(&list, i);
|
||||
list_print(&list);
|
||||
}
|
||||
|
||||
printf("----- delete -----\n");
|
||||
list_delete(&list, 17);
|
||||
list_print(&list);
|
||||
|
||||
list_delete(&list, 0);
|
||||
list_print(&list);
|
||||
|
||||
list_delete(&list, 9);
|
||||
list_print(&list);
|
||||
|
||||
printf("----- clear -----\n");
|
||||
list_clear(&list);
|
||||
printf("----- insert -----\n");
|
||||
for (i = 0; i < 18; i++)
|
||||
{
|
||||
list_insert(&list, 0, i);
|
||||
list_print(&list);
|
||||
}
|
||||
|
||||
list_print(&list);
|
||||
list_destory(&list);
|
||||
}
|
||||
|
@ -3,6 +3,10 @@
|
||||
|
||||
#include "common.h"
|
||||
|
||||
// #define LINK_LIST 1
|
||||
#define LIST 1
|
||||
|
||||
#ifdef LINK_LIST
|
||||
#define LIST_TEST
|
||||
|
||||
#ifdef LIST_TEST
|
||||
@ -32,8 +36,39 @@ uint32_t list_count(plist_t list);
|
||||
void list_traversal_sequence(plist_t list, list_data_disp_t disp);
|
||||
void list_traversal_reversed(plist_t list, list_data_disp_t disp);
|
||||
|
||||
extern void list_test(void);
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef LIST
|
||||
|
||||
typedef int list_data_t;
|
||||
|
||||
struct _list
|
||||
{
|
||||
list_data_t * array; // 数组(存储列表元素)
|
||||
int capacity; // 列表容量
|
||||
int size; // 列表大小
|
||||
int extend_ratio; // 列表每次扩容的倍数
|
||||
};
|
||||
typedef struct _list * list_t;
|
||||
|
||||
bool list_init(list_t list);
|
||||
void list_destory(list_t list);
|
||||
|
||||
bool list_empty(list_t list);
|
||||
int list_size(list_t list);
|
||||
|
||||
bool list_insert(list_t list, int index, list_data_t data);
|
||||
bool list_append(list_t list, list_data_t data);
|
||||
list_data_t list_delete(list_t list, int index);
|
||||
bool list_clear(list_t list);
|
||||
list_data_t list_get(list_t list, int index);
|
||||
bool list_set(list_t list, int index, list_data_t data);
|
||||
|
||||
extern void list_test(void);
|
||||
|
||||
#endif
|
||||
|
||||
#endif // _LIST_H_
|
||||
|
@ -3,13 +3,15 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
while (1)
|
||||
list_test();
|
||||
|
||||
// while (1)
|
||||
{
|
||||
//stack_test();
|
||||
//queue_test();
|
||||
// list_test();
|
||||
//tree_test();
|
||||
rbtree_test();
|
||||
// rbtree_test();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user