在Java中動(dòng)態(tài)數(shù)組為ArrayList, C++ STL中為Vector. JAVA,C++中均采用泛型來實(shí)現(xiàn),實(shí)現(xiàn)了數(shù)據(jù)結(jié)構(gòu)與存儲(chǔ)類型的分離。在C語言中沒有泛型。故采用void 指針來實(shí)現(xiàn)泛型的效果。
站在用戶的角度思考問題,與客戶深入溝通,找到石阡網(wǎng)站設(shè)計(jì)與石阡網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋石阡地區(qū)。arraylist.h
#ifndef ARRAY_LIST_H
#define ARRAY_LIST_H
#include
#include
#include
typedef void MyArrayListNode;
typedef struct _ArrayList {
int length; //當(dāng)前長(zhǎng)度
int capacity; //容量
unsigned int* data;//數(shù)組指針
}MyArrayList;
//創(chuàng)建并且返回一個(gè)空的ArrayList
MyArrayList* ArrayList_Create(int cap);
//銷毀一個(gè)線性表ArrayList
void ArrayList_Destroy(MyArrayList* list);
//將一個(gè)線性表ArrayList中的所有元素清空
void ArrayList_Clear(MyArrayList* list);
//返回一個(gè)線性表ArrayList中的所有元素個(gè)數(shù)
int ArrayList_Length(MyArrayList* list);
//向一個(gè)線性表ArrayList的pos位置處插入新元素node
int ArrayList_Insert(MyArrayList* list, MyArrayListNode* node, int pos);
//獲取一個(gè)線性表ArrayList的pos位置處的元素
MyArrayListNode* ArrayList_Get(MyArrayList* list, int pos);
//刪除一個(gè)線性表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;
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。