c语言版仿c++封闭动态数组...
时间:2010-08-18 来源:zhaogengzi
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef int DataType; //是这样用吗?
struct DArray
{
int capacity; //数组容量
DataType *parray; //动态数组其实就是一个指向一块内在的指针.
int count; //已使用的元素个数
void (*init_array)(struct DArray *pthis, int size); //初始化数组的大小.
void (*insert)(struct DArray *pthis, int index, DataType element);
DataType (*get_element)(struct DArray *pthis, int index);
void (*change_size)(struct DArray *pthis, int newsize);
int (*get_capacity)(struct DArray *pthis);
void (*free)(struct DArray *pthis);
}; //结构体后面的分号一定不能再忘了..
void array_change_size(struct DArray *pthis, int newsize)
{
int limit = 0;
int i;
if((newsize<=0) || (newsize==pthis->capacity))
{
printf("array_change_size error!: newsize <=0 || newsize == pthis->capacity \n");
return;
}
DataType *pnew = (DataType*)malloc(sizeof(DataType)*newsize);
if(pnew==NULL)
{
printf("memory exhaust!\n");
exit(1);
}
limit = (newsize > pthis->capacity) ? pthis->capacity : newsize;
for(i=0; i<limit; i++)
{
pnew[i] = (pthis->parray)[i];
}
free(pthis->parray);
pthis->parray = pnew;
pthis->capacity = newsize;
}
void array_insert(struct DArray *pthis, int index, DataType element) //在添加元素时就是这样.
{
#ifdef DEBUGARRAY //用于对数组的索引进行调试
if(index < 0)
{
printf("array_insert error : index < 0\n");
exit(1);
}
#endif
(pthis->parray)[index] = element;
++pthis->count; //记录数组中有效元素个数
}
DataType array_get_element(struct DArray *pthis, int index)
{
#ifdef DEBUGARRAY //用于对数组的索引进行调试
if(index < 0)
{
printf("array_insert error : index < 0\n");
exit(1);
}
#endif
--pthis->count;
return (pthis->parray)[index];
}
void array_free(struct DArray *pthis)
{
free(pthis->parray);
pthis->parray = NULL; //别记了这步很关键
pthis->capacity = 0;
pthis->count = 0;
}
int array_get_capacity(struct DArray *pthis)
{
return pthis->capacity;
}
void array_init_array(struct DArray *pthis, int size)
{
int capacity;
capacity = (size>1)?size:2; //数组初始化时容量必须为2的倍数
pthis->capacity = capacity;
pthis->parray = (DataType *)malloc(sizeof(DataType)*capacity);
if(pthis->parray == NULL)
{
printf("memory exhoust!\n");
exit(1);
}
pthis->count = 0;
pthis->init_array = array_init_array; //初始化所有的函数指针.
pthis->insert = array_insert;
pthis->get_element = array_get_element;
pthis->change_size = array_change_size;
pthis->free = array_free;
pthis->get_capacity = array_get_capacity;
}
int main()
{
struct DArray array;
array_init_array(&array,2); //初始大小为2的倍数
for(int i=0; i<1000; i++)
{
if(i == array.get_capacity(&array))
{
array.change_size(&array, i<<1); //如果满了数组容量加大两倍
}
array.insert(&array, i, i);
}
int trysize = array.get_capacity(&array); //数组容量.
int num = 0;
cout<<"trysize:"<<trysize<<endl;
for(int j=999; j>-1; j--)
{
num = j+1; //元素个数,这种错误的原因是马虎吗?吗?吗?要不就是那个指针的位置没有加星,在进行代码copy时一定要注意,变量名问题,脑子一定要想着我在哪里用过这些变量.
while(num<=trysize>>2 && trysize>2)
{
trysize >>=1;
cout<<"newsize::"<<trysize<<endl;
}
if(trysize<array.get_capacity(&array))
{
//cout<<"newsize::"<<trysize<<endl;
array.change_size(&array, trysize);
}
cout<<array.get_element(&array, j)<<" ";
}
cout<<endl;
return 0;
}