?? 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 + -