C語(yǔ)言構(gòu)建類似java的動(dòng)態(tài)數(shù)組
1、定義
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <malloc.h>
#define InitSize 10 //初始化長(zhǎng)度
typedef int ElemType ;
struct _ArrayList{
ElemType *node;//節(jié)點(diǎn)
int length;//鏈表長(zhǎng)度
int size;//鏈表有效個(gè)數(shù)大小
};
typedef struct _ArrayList ArrayList;
2、初始化
/**
* 功能:初始化鏈表
* 參數(shù):鏈表地址
* 返回值:如果成功,true,如果失敗 false
*/
bool Init_List(ArrayList *pList){
pList->node=(ElemType*)malloc(sizeof(ElemType)*InitSize);//分配節(jié)點(diǎn)初始化空間
if(pList->node==NULL){
printf("pList->node動(dòng)態(tài)內(nèi)存分配失敗!\n");
return false;
}
pList->length=InitSize;
pList->size=0;
printf("初始化成功\n");
return true;
}
3、判斷空表
/**
* 功能:判斷鏈表是否是空表
* 參數(shù):鏈表地址
* 返回值:true 是 false 否
*/
bool IsEmpty(ArrayList *pList){
if(pList->size==0){
return true;
}
return false;
}
4、獲取已分配的長(zhǎng)度
/**
* 功能:獲取已分配的長(zhǎng)度
* 參數(shù):鏈表地址
* 返回值
*/
int Length_List(ArrayList *pList){
return pList->length;
}
5、獲取有效長(zhǎng)度
/**
* 功能:獲取有效長(zhǎng)度
* 參數(shù):鏈表地址
* 返回值
*/
int Size_List(ArrayList *pList){
return pList->size;
}
6、插入操作
/**
* 功能:向鏈表插入數(shù)據(jù)元素 前置條件:鏈表已初始化,1<=index<=pList->size+1
* 參數(shù):鏈表地址 下標(biāo) 插入的元素
* 返回值:鏈表長(zhǎng)度
*/
bool Insert_List(ArrayList *pList,int index, ElemType e){
if(pList->node==NULL){
printf("鏈表未初始化\n");
return false;
}
if(index<1||index>pList->size+1){
printf("下標(biāo)越界\n");
return false;
}
//判斷是否需要?jiǎng)討B(tài)添加空間
if(pList->length==pList->size){
int newLength=(pList->length*3)/2+1;
pList->node=realloc(pList->node,sizeof(ElemType)*newLength);
pList->length=newLength;
}
if(!IsEmpty(pList) && index<pList->size+1){
//插入數(shù)據(jù)時(shí)從最后一個(gè)開始到index的位置向后面移動(dòng)一個(gè)位置
for(int i=pList->size;i>=index;i--){
pList->node[i]=pList->node[i-1];
}
}
pList->node[index-1]=e;
pList->size++;
return true;
}
7、刪除操作
/**
* 功能:刪除某個(gè)下標(biāo)的值 前置條件:鏈表已初始化,1<=index<=pList->size
* 參數(shù):鏈表地址 下標(biāo) 返回刪除的長(zhǎng)度
* 返回值:true 成功 false 失敗
*/
bool Delete_List(ArrayList *pList,int index, ElemType *e){
if(pList->node==NULL){
printf("鏈表未初始化\n");
return false;
}
if(index<1||index>pList->size){
printf("下標(biāo)越界\n");
return false;
}
*e=pList->node[index-1];
for(int i=index-1;i<pList->size-1;i++){
pList->node[i]=pList->node[i+1];
}
pList->size--;
if((pList->length-pList->size)>(InitSize*2)){
int newLength=pList->length-InitSize;
pList->node=realloc(pList->node,sizeof(ElemType)*newLength);
pList->length=newLength;
}
return true;
}
8、查找元素
/**
* 功能:查找元素,返回下標(biāo)
* 參數(shù):鏈表地址 元素
* 返回值: 下標(biāo) -1 為查找不到
*/
int Search_List(ArrayList *pList,ElemType e){
if(IsEmpty(pList)){
return -1;
}
for(int i=0;i<pList->size;i++){
if(pList->node[i]==e){
return i+1;
}
}
return -1;
}
9、獲取下標(biāo)的元素
/**
* 功能:獲取某個(gè)下標(biāo)的元素
* 參數(shù):鏈表地址 下標(biāo)
* 返回值: 元素
*/
bool GetElem(ArrayList *pList,int index,ElemType *e){
if(IsEmpty(pList)){
return false;
}
if(index<1||index>pList->size){
printf("數(shù)組下標(biāo)越界\n");
return false;
}
*e=pList->node[index-1];
return true;
}
10、遍歷
/**
* 功能:遍歷鏈表
* 參數(shù):鏈表地址
*/
void Show_List(ArrayList *pList){
if(IsEmpty(pList)){
printf("[]\n");
return ;
}
bool flag=true;
printf("[");
for(int i=0;i<pList->size;i++){
if(flag){
printf("%d",pList->node[i]);
flag=false;
}else{
printf(",%d",pList->node[i]);
}
}
printf("]\n");
}
11、追加元素
/**
* 功能:向鏈表追加一個(gè)數(shù)據(jù)元素
* 參數(shù):鏈表地址 插入的元素
* 返回值:true false
*/
bool Append_List(ArrayList *pList, ElemType e){
return Insert_List(pList,pList->size+1,e);
}
12、清空線性表
/**
* 功能:清空線性表
* 參數(shù):pList:鏈表地址
* 返回值:true false
*/
bool List_Clear(ArrayList *pList){
ElemType* node=pList->node;
pList->node=NULL;
free(node);
pList->length=0;
pList->size=0;
return true;
}
測(cè)試
int main()
{
ArrayList pList;
Init_List(&pList);
Insert_List(&pList,1,5);
Insert_List(&pList,1,4);
Insert_List(&pList,1,5);
Insert_List(&pList,1,7);
Insert_List(&pList,1,7);
Insert_List(&pList,1,4);
Insert_List(&pList,1,7);
Insert_List(&pList,1,56);
Insert_List(&pList,1,8);
Insert_List(&pList,1,3);
Insert_List(&pList,7,3);
printf("length:%d,size:%d\n",pList.length,pList.size);
Insert_List(&pList,1,4);
printf("length:%d,size:%d\n",pList.length,pList.size);
Insert_List(&pList,1,1);
printf("length:%d,size:%d\n",pList.length,pList.size);
Show_List(&pList);
int val;
Delete_List(&pList,5,&val);
printf("刪除的元素%d\n",val);
printf("length:%d,size:%d\n",pList.length,pList.size);
Show_List(&pList);
printf("開始清除\n");
List_Clear(&pList);
Show_List(&pList);
Append_List(&pList,10000);
Init_List(&pList);
Append_List(&pList,23432);
Show_List(&pList);
int index=Search_List(&pList,3);
printf("下標(biāo)為%d\n",index);
GetElem(&pList,5,&val);
printf("獲取的元素%d\n",val);
return 0;
}