使用C实现类似Java ArrayList数据结构

来源:互联网 发布:iphone改内存容量数据 编辑:程序博客网 时间:2024/06/09 18:07

使用C实现类型Java ArrayList数据结构
方法文档

void init(struct Array *pArr, int len);初始化void toString(struct Array *pArr);bool empty(struct Array *pArr);bool full(struct Array *pArr);bool append(struct Array *pArr, int val); 末尾插入bool insert(struct Array *pArr, int index, int val); 指定位置插入bool del(struct Array *pArr, int index);删除指定位置void reversion(struct Array *pArr);倒置void sort(struct Array *pArr);

实现代码

#include <stdio.h>#include<malloc.h>struct Array{    int * pBase;    int cnt;    int len;};void init(struct Array *pArr, int len);void toString(struct Array *pArr);bool empty(struct Array *pArr);bool full(struct Array *pArr);bool append(struct Array *pArr, int val);bool insert(struct Array *pArr, int index, int val);bool del(struct Array *pArr, int index);void reversion(struct Array *pArr);void sort(struct Array *pArr);int main(void){    struct Array arr;    init(&arr, 10);    printf("init");    toString(&arr);    append(&arr, 44);    append(&arr, 11);    append(&arr, 77);    printf("append:44,11,77");    toString(&arr);    insert(&arr, 1, 22);    printf("insert:1,22");    toString(&arr);    insert(&arr, 3, 55);    printf("insert:3,55");    toString(&arr);    del(&arr, 2);    printf("del:2");    toString(&arr);    reversion(&arr);    printf("reversion");    toString(&arr);    sort(&arr);    printf("sort");    toString(&arr);    getchar();    return 0;}void init(struct Array *pArr, int len){    pArr->pBase = (int *)malloc(sizeof(int)* len);    pArr->cnt = 0;    pArr->len = len;}bool append(struct Array *pArr, int val){    if (full(pArr)) return false;    pArr->pBase[pArr->cnt] = val;    pArr->cnt++;    return true;}bool insert(struct Array *pArr, int index, int val){    int i;    if (full(pArr)) return false;    for (i = pArr->cnt; i >index; --i)    {        pArr->pBase[i] = pArr->pBase[i-1];    }    pArr->pBase[index] = val;    pArr->cnt++;    return true;}bool del(struct Array *pArr, int index){    int i;    if (empty(pArr)) return false;    for (i = index; i <pArr->cnt; ++i)    {        pArr->pBase[i] = pArr->pBase[i+1];    }       pArr->cnt--;    return true;}void reversion(struct Array *pArr){    if (empty(pArr)) return;    int i = 0;    int j = pArr->cnt-1;    int t;    while (i < j)    {        t = pArr->pBase[i];        pArr->pBase[i] = pArr->pBase[j];        pArr->pBase[j] = t;        ++i;        --j;    }}void sort(struct Array *pArr){    if (empty(pArr)) return;    int i,j,t;    for (i = 0; i < pArr->cnt; ++i)    {        for (j = i; j < pArr->cnt; ++j)        {            if (pArr->pBase[i] > pArr->pBase[j]){                t = pArr->pBase[j];                pArr->pBase[j] = pArr->pBase[i];                pArr->pBase[i] = t;            }        }    }}void toString(struct Array *pArr){    int i;    if (empty(pArr))    {        printf("[] \n");        return;    }    printf("[");    for (i = 0; i < pArr->cnt; i++)    {        printf("%d ,", pArr->pBase[i]);    }    printf("] \n");    return;}bool empty(struct Array *pArr){    if (pArr->cnt > 0) return false;    else return true;}bool full(struct Array *pArr){    if (pArr->cnt >= pArr->len)        return true;    else        return false;}

运行结果:

init[]append:44,11,77[44 ,11 ,77 ,]insert:1,22[44 ,22 ,11 ,77 ,]insert:3,55[44 ,22 ,11 ,55 ,77 ,]del:2[44 ,22 ,55 ,77 ,]reversion[77 ,55 ,22 ,44 ,]sort[22 ,44 ,55 ,77 ,]
原创粉丝点击