⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arraylist.c

📁 C语言编写的arraylist
💻 C
字号:
#include "arraylist.h"


int listAdd(struct ArrayList* list,void* object)
{
	int i=0;
	if(list->count >= list->capacity)
	{
		if(list->capacity == 0)
		{
			list->objects = malloc( 4 * sizeof(int) );
			list->capacity = 4;
		}
		else
		{
			list->objects = realloc( list->objects, list->capacity * 2 * sizeof(int)) ;
			list->capacity *= 2;			
		}
	}
	list->objects[list->count] = (int)object;
	list->count++;
	return list->count;
}
void listClear(struct ArrayList* list)
{
	free(list->objects);
	list->objects = 0;
	list->capacity = 0;
	list->count = 0;
}
void listRemove(struct ArrayList* list,void* object)
{
	int i=0;
	int count = list->count;
	int *objects = list->objects;
	int j;
	for(;i<count;i++)
	{
		if(objects[i]==(int)object)
		{
			for(j=i;j<count;j++)
			{
				objects[j]=objects[j+1];
			}
			list->count--;
			return;
		}
	}
}
void listRemoveAt(struct ArrayList* list,int index)
{
	int count = list->count;
	int *objects = list->objects;
	int i;
	if(index>count-1||index<0)
	{
		return;
	}
	for(i=index;i<count;i++)
	{
		objects[i]=objects[i+1];
	}
	list->count--;
}

int contain(const struct ArrayList* list,void* object)
{
	int count=list->count;
	int *objects = list->objects;
	int i,flag=0;

	for(i=0;i<count;i++){
		if(objects[i]==(int)object)
			flag=1;
	}

	return flag;
}

int indexOf(const struct ArrayList* list,void* object)
{
	int i,index=-1;

	if(contain(list,object)){
		for(i=0;i<list->count;i++)
			if(list->objects[i]==(int)object)
				index=i;
	}

	return index;
}

///////////////////////////////////////double型动态数组/////////////////////////////////////////////////

vector* createVector()
{
	vector* p=malloc(sizeof(vector));
	p->capacity=0;
	p->count=0;
	p->objects=0;

	return p;
}

void vectorClear(vector* p)
{
	free(p->objects);
	p->objects=0;
	p->count=0;
	p->capacity=0;
}

void vectorAdd(vector* p,double object)
{
	if(p->count>=p->capacity){
		if(p->capacity==0){
			p->objects=malloc(sizeof(double)*4);
			p->capacity=4;
		}else{
			p->objects=realloc(p->objects,sizeof(double)*p->capacity*2);
			p->capacity*=2;
		}
	}

	p->objects[p->count++]=object;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -