mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
dynamic array data structure
This commit is contained in:
parent
88de94d1b4
commit
c3e10d552a
13
data_structures/dynamic_array/Makefile
Normal file
13
data_structures/dynamic_array/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CC = gcc
|
||||||
|
CFLAGS = -g -Wall
|
||||||
|
|
||||||
|
all: main
|
||||||
|
|
||||||
|
main: main.o dynamic_array.o
|
||||||
|
$(CC) $(CFLAGS) $^ -o $@
|
||||||
|
|
||||||
|
dynamic_array.o: dynamic_array.c
|
||||||
|
$(CC) $(CFLAGS) -c $^
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm *.o main
|
79
data_structures/dynamic_array/dynamic_array.c
Normal file
79
data_structures/dynamic_array/dynamic_array.c
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "dynamic_array.h"
|
||||||
|
|
||||||
|
dynamic_array_t *init_dynamic_array()
|
||||||
|
{
|
||||||
|
dynamic_array_t *da = malloc(sizeof(dynamic_array_t));
|
||||||
|
da->items = calloc(DEFAULT_CAPACITY, sizeof(void *));
|
||||||
|
da->capacity = DEFAULT_CAPACITY;
|
||||||
|
|
||||||
|
return da;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *add(dynamic_array_t *da, const void *value)
|
||||||
|
{
|
||||||
|
if (da->size >= da->capacity) {
|
||||||
|
void **newItems = realloc(da->items, (da->capacity <<= 1) * sizeof(void **));
|
||||||
|
free(da->items);
|
||||||
|
|
||||||
|
da->items = newItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *copy_value = retrive_copy_of_value(value);
|
||||||
|
da->items[da->size++] = copy_value;
|
||||||
|
|
||||||
|
return copy_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *put(dynamic_array_t *da, const void *value, const unsigned index)
|
||||||
|
{
|
||||||
|
if (!contains(da->size, index))
|
||||||
|
return INDEX_OUT_OF_BOUNDS;
|
||||||
|
|
||||||
|
free(da->items[index]);
|
||||||
|
void *copy_value = retrive_copy_of_value(value);
|
||||||
|
da->items[index] = copy_value;
|
||||||
|
|
||||||
|
return copy_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *get(dynamic_array_t *da, const unsigned index)
|
||||||
|
{
|
||||||
|
if (!contains(da->size, index))
|
||||||
|
return INDEX_OUT_OF_BOUNDS;
|
||||||
|
|
||||||
|
return da->items[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
void delete (dynamic_array_t *da, const unsigned index)
|
||||||
|
{
|
||||||
|
if (!contains(da->size, index))
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (unsigned i = index; i < da->size; i++) {
|
||||||
|
da->items[i] = da->items[i + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
da->size--;
|
||||||
|
|
||||||
|
free(da->items[da->size]);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned contains(const unsigned size, const unsigned index)
|
||||||
|
{
|
||||||
|
if (size >= 0 && index < size)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
printf("index [%d] out of bounds!\n", index);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *retrive_copy_of_value(const void *value)
|
||||||
|
{
|
||||||
|
void *value_copy = malloc(sizeof(void *));
|
||||||
|
memcpy(value_copy, value, sizeof(void *));
|
||||||
|
|
||||||
|
return value_copy;
|
||||||
|
}
|
26
data_structures/dynamic_array/dynamic_array.h
Normal file
26
data_structures/dynamic_array/dynamic_array.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#ifndef __DYNAMIC_ARRAY__
|
||||||
|
#define __DYNAMIC_ARRAY__
|
||||||
|
#define DEFAULT_CAPACITY 1 << 4
|
||||||
|
#define INDEX_OUT_OF_BOUNDS NULL
|
||||||
|
|
||||||
|
typedef struct dynamic_array {
|
||||||
|
void **items;
|
||||||
|
unsigned size;
|
||||||
|
unsigned capacity;
|
||||||
|
} dynamic_array_t;
|
||||||
|
|
||||||
|
extern dynamic_array_t *init_dynamic_array();
|
||||||
|
|
||||||
|
extern void *add(dynamic_array_t *da, const void *value);
|
||||||
|
|
||||||
|
extern void *put(dynamic_array_t *da, const void *value, unsigned index);
|
||||||
|
|
||||||
|
extern void *get(dynamic_array_t *da, const unsigned index);
|
||||||
|
|
||||||
|
extern void delete (dynamic_array_t *da, const unsigned index);
|
||||||
|
|
||||||
|
unsigned contains(const unsigned size, const unsigned index);
|
||||||
|
|
||||||
|
extern void *retrive_copy_of_value(const void *value);
|
||||||
|
|
||||||
|
#endif
|
33
data_structures/dynamic_array/main.c
Normal file
33
data_structures/dynamic_array/main.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include "dynamic_array.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
dynamic_array_t *da = init_dynamic_array();
|
||||||
|
|
||||||
|
for (int i = 1; i <= 50; i++) {
|
||||||
|
add(da, &i);
|
||||||
|
}
|
||||||
|
|
||||||
|
delete (da, 10);
|
||||||
|
|
||||||
|
int value = 1000;
|
||||||
|
|
||||||
|
put(da, &value, 0);
|
||||||
|
|
||||||
|
value = 5000;
|
||||||
|
|
||||||
|
int another_value = 7000;
|
||||||
|
|
||||||
|
add(da, &another_value);
|
||||||
|
|
||||||
|
for (int i = 0; i < da->size; i++) {
|
||||||
|
printf("value %d\n", *(int *)get(da, i));
|
||||||
|
}
|
||||||
|
|
||||||
|
int value_for_invalid_index = 10000;
|
||||||
|
|
||||||
|
put(da, &value_for_invalid_index, 150);
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user