我的C算法库[5]:实现数据结构ArrayList

1.arraylist.h

/* * arraylist.h * * Created on: 2010-7-15 * Author: qibaoyuan */ #ifndef ARRAYLIST_H_ #define ARRAYLIST_H_ #ifdef __cplusplus extern "C" { #endif /** * value to be stored in the ArrayList */ typedef void* ArrayListValue; /** * the structer */ typedef struct _ArrayList ArrayList; /** * definition of ArrayList */ struct _ArrayList { /** * entries in the array */ ArrayListValue *data; /** * length of the array */ int length; /** * private data */ int _alloced; }; /** * compare function between two values ih the array */ typedef int (*ArrayListEqualFunc)(ArrayListValue value1, ArrayListValue value2); ArrayList *arrayList_new(int length); void arrayList_free(ArrayList *arrayList); int arrayList_append(ArrayList *arrayList, ArrayListValue data); /** * Remove the entry at the specified location in an ArrayList. * * @param arraylist The ArrayList. * @param index The index of the entry to remove. */ void arraylist_remove(ArrayList *arraylist, int index); /** * Remove a range of entries at the specified location in an ArrayList. * * @param arraylist The ArrayList. * @param index The index of the start of the range to remove. * @param length The length of the range to remove. */ void arraylist_remove_range(ArrayList *arraylist, int index, int length); int arraylist_insert(ArrayList *arraylist, int index, ArrayListValue data); void dump(ArrayList *arraylist); #ifdef __cplusplus } #endif #endif /* ARRAYLIST_H_ */

2.arraylist.c

/* * arraylist.c * * Created on: 2010-7-15 * Author: qibaoyuan */ #include #include #include #include #include #include "arraylist.h" ArrayList *arrayList_new(int length) { ArrayList *new_arrayList; if (length <= 0) length = 16; new_arrayList = (ArrayList *) malloc(sizeof(ArrayList)); if (new_arrayList == NULL) return NULL; //alloced space new_arrayList->_alloced = length; //the arrays that contains data is 0 new_arrayList->length = 0; new_arrayList->data = malloc(length * sizeof(ArrayListValue)); if (new_arrayList->data == NULL) { free(new_arrayList); return NULL; } return new_arrayList; } void arrayList_free(ArrayList *arrayList) { if (arrayList != NULL) { free(arrayList->data); free(arrayList); } } static int arraylist_enlarge(ArrayList *arraylist) { ArrayListValue *data; int newsize = arraylist->_alloced * 2; data = realloc(arraylist->data, sizeof(ArrayListValue) * newsize); if (data == NULL) { return 0; } else { arraylist->data = data; arraylist->_alloced = newsize; return 1; } } /** * Insert a value at the specified index in an ArrayList. * The index where the new value can be inserted is limited by the * size of the ArrayList. * * @param arraylist The ArrayList. * @param index The index at which to insert the value. * @param data The value. * @return Returns zero if unsuccessful, else non-zero * if successful (due to an invalid index or * if it was impossible to allocate more memory). */ int arraylist_insert(ArrayList *arraylist, int index, ArrayListValue data) { if (index < 0 || index > arraylist->length) { return 0; } if (arraylist->length + 1 > arraylist->_alloced) { if (!arraylist_enlarge(arraylist)) return 0; } memmove(&arraylist->data[index + 1], &arraylist->data[index], (arraylist->length - index) * sizeof(ArrayListValue)); arraylist->data[index] = data; ++arraylist->length; return 1; } int arrayList_append(ArrayList *arraylist, ArrayListValue data) { return arraylist_insert(arraylist, arraylist->length, data); } void arraylist_remove_range(ArrayList *arraylist, int index, int length) { if (index < 0 || length < 0 || index + length > arraylist->length) { return; } memmove(&arraylist->data[index], &arraylist->data[index + length], sizeof(ArrayListValue) * (arraylist->length - length - index)); arraylist->length -= length; } void arraylist_remove(ArrayList *arraylist, int index) { arraylist_remove_range(arraylist, index, 1); } void dump(ArrayList *arraylist) { if (arraylist == NULL) return; int i = 0; for (i = 0; i < arraylist->length; i++) { printf("%d ", (int) *((int*) arraylist->data[i])); } }

3.测试函数

 /* ============================================================================ Name : Algorithm.c Author : 齐保元 Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include #include #include //#include "hashmap.h" #include "arraylist.h" int main(void) { // HashMap *map = (HashMap*) malloc(sizeof(HashMap)); // hashmap_open(map, 5); // char* key = "qibaoyuan"; // char* value = "test"; // char* key1 = "qibaoyuan"; // char* value1 = "test1"; // char* key2 = "qibaoyuan2"; // char* value2 = "test"; // hashmap_put(map, key, NULL, value, NULL); // hashmap_put(map, key1, NULL, value1, NULL); // hashmap_put(map, key2, NULL, value2, NULL); // printf("/n%s:", (char*) hashmap_get(map, key, NULL)); // hashmap_dump(map); int k1 = 23, k2 = 4, k3 = 5, k4 = 90; ArrayList *arraylist = arrayList_new(10); arrayList_append(arraylist, &k1); arrayList_append(arraylist, &k2); arrayList_append(arraylist, &k3); arrayList_append(arraylist, &k4); arrayList_append(arraylist, &k3); arraylist_remove(arraylist, 2); dump(arraylist); arrayList_free(arraylist); return EXIT_SUCCESS; }

你可能感兴趣的:(数据结构/算法,数据结构,算法,c,hashmap,null,insert)