设为首页 加入收藏

TOP

c语言实现动态指针数组Dynamic arrays(五)
2015-01-22 21:02:36 来源: 作者: 【 】 浏览:105
Tags:语言 实现 动态 指针 Dynamic arrays
ze); if(data != NULL) { //clear the expanded space of the new memory. MEMSETFUN(data+pArr->m_nAllocSize,0,(allocSize-pArr->m_nAllocSize)*sizeof(void*)); pArr->m_ppData = data; pArr->m_nAllocSize = allocSize; } } return ((pArr->m_nSize + nNeed) <= pArr->m_nAllocSize) ? CP_TRUE : CP_FALSE; } /************************************************************************************************** 【函数名】: DyArrayInsert 【描述】:插入数组by index 【参数】: pArr: the array's address. nIndex: the position in the array to insert new data. pData: the data to be inserted. 【返回值】:如果成功返回true否则false ***************************************************************************************************/ cp_bool DyArrayInsert(DyArray* pArr, cp_int32 nIndex, void* pData) { cp_bool bRet = CP_FALSE; cp_int32 nCursor = nIndex; cp_int32 i; //if the input parameter is invalid, return. if(!pArr) { return CP_FALSE; } //get the right cursor. nCursor = nCursor < pArr->m_nSize ? nCursor : pArr->m_nSize; if(DyArrayExpand(pArr, 1) == CP_TRUE) { //move all the elements after the cursor to the next positon. for(i = pArr->m_nSize; i > nCursor; i--) { pArr->m_ppData[i] = pArr->m_ppData[i-1]; } //set the cursor's value. pArr->m_ppData[nCursor] = pData; pArr->m_nSize++; bRet = CP_TRUE; } return bRet; } /************************************************************************************************** 【函数名】: DyArrayPrepend 【描述】:在数组开始添加一个数据 【参数】: pArr: the array's address. pData: the data to be added. 【返回值】:如果成功返回ture,否则false ***************************************************************************************************/ cp_bool DyArrayPrepend(DyArray* pArr, void* pData) { return DyArrayInsert(pArr,0,pData) ? CP_TRUE:CP_FALSE; } /************************************************************************************************** 【函数名】: DyArrayAppend 【描述】:添加数据到数组末尾 【参数】: pArr: the array's address. pData: the data to be added. 【返回值】:如果成功返回true,否则false ***************************************************************************************************/ cp_bool DyArrayAppend(DyArray* pArr, void* pData) { return DyArrayInsert(pArr,pArr->m_nSize,pData) ? CP_TRUE:CP_FALSE; } /************************************************************************************************** 【函数名】: DyArrayShrink 【描述】:缩减数组 【参数】: pArr: the array's address. 【返回值】:ture 或者false ***************************************************************************************************/ cp_bool DyArrayShrink(DyArray* pArr) { cp_int32 nAllocSize = 0; void** pData = NULL; //if the input 【参数】 is invalid, return. if(!pArr) { return CP_FALSE; } //if need, shrink the array to 1.5 times of elements number. if((pArr->m_nSize < (pArr->m_nAllocSize >> 1)) && (pArr->m_nAllocSize > MIN_PRE_ALLOCATE_SIZE)) { nAllocSize = pArr->m_nSize + (pArr->m_nSize >> 1); pData = (void**)REALLOCFUN(pArr->m_ppData, sizeof(void*) * nAllocSize); if(pData != NULL) { //clear memory of the unused space of new memory. MEMSETFUN(pData+pArr->m_nSize,0,(nAllocSize-pArr->m_nSize)*sizeof(void*)); pArr->m_ppData = pData; pArr->m_nAllocSize = nAllocSize; } } return CP_TRUE; } /************************************************************************************************** 【函数名】: DyArrayDelete 【描述】:删除元素by index 【参数】: pArr: the array's address. nIndex: the position in the array to delete useless data. 【返回值】:ture 或者false *
首页 上一页 2 3 4 5 6 下一页 尾页 5/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C和指针 (pointers on C)――第.. 下一篇C专家编程之编译器日期被破坏程序

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: