C语言实现ArrayList (线性表顺序存储结构)

发布时间:2020-08-03 10:13:34 作者:暮色伊人
来源:网络 阅读:2231

在Java中动态数组为ArrayList, C++ STL中为Vector. JAVA,C++中均采用泛型来实现,实现了数据结构与存储类型的分离。在C语言中没有泛型。故采用void 指针来实现泛型的效果。

arraylist.h

#ifndef ARRAY_LIST_H
#define ARRAY_LIST_H
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>

typedef void MyArrayListNode;
typedef struct _ArrayList {
    int length;         //当前长度
    int capacity;      //容量
    unsigned int* data;//数组指针
}MyArrayList;

//创建并且返回一个空的ArrayList
MyArrayList* ArrayList_Create(int cap);

//销毁一个线性表ArrayList
void ArrayList_Destroy(MyArrayList* list);

//将一个线性表ArrayList中的所有元素清空
void ArrayList_Clear(MyArrayList* list);

//返回一个线性表ArrayList中的所有元素个数
int ArrayList_Length(MyArrayList* list);

//向一个线性表ArrayList的pos位置处插入新元素node
int ArrayList_Insert(MyArrayList* list, MyArrayListNode* node, int pos);

//获取一个线性表ArrayList的pos位置处的元素
MyArrayListNode* ArrayList_Get(MyArrayList* list, int pos);

//删除一个线性表ArrayList的pos位置处的元素  返回值为被删除的元素,NULL表示删除失败
MyArrayListNode* ArrayList_Delete(MyArrayList* list, int pos);

//增加ArrayList 空间大小,返回新空间大小
int grow(MyArrayList* list, int size);
#endif // !ARRAY_LIST_H

arraylist.c

#include"arraylist.h"

MyArrayList* ArrayList_Create(int capacity) {
    MyArrayList *list = NULL;
    list = (MyArrayList *)malloc(sizeof(MyArrayList));
    if (list == NULL) {
        fprintf(stderr,"create ArrayList failed.\n");
        return NULL;
    }
    memset(list, 0, sizeof(list));
    list->data = (unsigned int*)malloc(sizeof(unsigned int*) * capacity);
    if (list->data == NULL) {
        fprintf(stderr, "malloc data space failed.\n");
        return NULL;
    }
    list->capacity = capacity;
    list->length = 0;
    return  list;
}

void ArrayList_Destroy(MyArrayList* list) {
    if (list == NULL) {
        return;
    }
    if (list->data) {
        free(list->data);
    }
    free(list);
}

void ArrayList_Clear(MyArrayList* list) {
    if (list == NULL) {
        return;
    }
    list->length = 0;
}

int ArrayList_Length(MyArrayList* list) {
    MyArrayList* arrayList = (MyArrayList* )list;
    return arrayList->length;
}

int ArrayList_Insert(MyArrayList* list, MyArrayListNode* node, int pos) {

    if (list == NULL) {
        return -1;
        fprintf(stderr,"the ArrayList is Null\n");
    }
    if (pos < 0) {
        return -2;
        fprintf(stderr, "the ArrayList is Null\n");
    }
    if (list->length == list->capacity) {
        fprintf(stdout, "the ArrayList is Full,Grow Capacity\n");
        int ret = grow(list,list->capacity * 2);
        if (ret == -1) {
            return -3;
        }
    }

    for (int i = list->length; i > pos; i--) {
        list->data[i] = list->data[i - 1];
    }
    list->data[pos] = (unsigned int)node;
    list->length += 1;
    return 0;
}

MyArrayListNode* ArrayList_Get(MyArrayList* list, int pos) {
    if (list == NULL || pos < 0 || pos > list->length) {
        fprintf(stderr,"wrong arguments\n");
        return NULL;
    }
    return (MyArrayListNode*)list->data[pos];
}

MyArrayListNode* ArrayList_Delete(MyArrayList* list, int pos){
    if (list == NULL || pos < 0 || pos > list->length) {
        fprintf(stderr, "wrong arguments\n");
        return NULL;
    }
    MyArrayListNode* node = (MyArrayListNode*)list->data[pos];
    for (int i = pos + 1; i < list->length; i++) {
        list->data[i-1] = list->data[i];
    }
    list->length--;
    return node;
}

int grow(MyArrayList* list, int size) {
    if (list == NULL || size < list->capacity) {
        fprintf(stderr, "wrong argument.\n");
        return -1;
    }
    int newSize = size;
    if (size > 2 * list->capacity) {
        newSize = 2 * list->capacity;
    }
    unsigned int* oldData = list->data;
    unsigned int* newData = malloc(newSize * sizeof(unsigned int*));
    if (newData == NULL) {
        fprintf(stderr,"grow capacity failed.\n");
        return -1;
    }
    list->data = newData;
    memset(list->data,0,sizeof(list->data));
    memcpy(list->data, oldData, sizeof(oldData)*list->length);
    list->capacity = newSize;
    free(oldData);
    return newSize;
}
推荐阅读:
  1. c语言二分查找的实现
  2. 线性表(1):线性表顺序存储结构的php实现

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

数据结构c/c++实现arraylis lis st

上一篇:如何实现php rsa

下一篇:如何递归php删除文件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》