C语言中动态数组操作实践

来源:互联网 发布:网络大电影有效转化率 编辑:程序博客网 时间:2024/05/16 23:01
#include<stdio.h>#include<malloc.h>//包含了malloc函数#include<stdlib.h>//包含了exit函数//定义了一个数据类型,该数据类型的名字为struct Arraystruct Array{int * pBase;//存储的是数组中第一个元素的地址int len;//数组所能容纳的最大元素个数int cnt;//当前数组有效元素的个数};//函数声明void initArr(struct Array * pArr, int length);//初始化bool appendElement(struct Array * pArr, int value);//追加元素bool insertElement(struct Array * pArr, int position, int value);//插入元素,position从1开始bool deleteElement(struct Array * pArr, int position, int * pValue);//删除int getElement(struct Array * pArr, int position);//获取相应位置元素,positon从1开始bool isEmpty(struct Array * pArr);//是否为空bool isFull(struct Array * pArr);//是否已满void sortArr(struct Array * pArr);//冒泡排序void showArr(struct Array * pArr);//显示内容void inversionArr(struct Array * pArr);//倒置int main(void){ //定义一个数组类型的变量struct Array arr;//删除时,返回删除的元素int value;//调用初始化数组的函数initArr(&arr, 6);//调用显示数组元素的函数showArr(&arr);//调用向数组中添加元素的函数appendElement(&arr, 56);appendElement(&arr, 100);appendElement(&arr, 23);appendElement(&arr, 12);appendElement(&arr, 72);//调用获取指定位置处元素的方法,并输出printf("第2个元素的值为:%d\n", getElement(&arr, 2));//调用插入元素的函数insertElement(&arr, 2, 99);//调用删除元素的函数if(deleteElement(&arr, 1, &value)){printf("删除成功\n");printf("您删除的元素为: %d\n", value);}elseprintf("删除失败!\n");showArr(&arr);//调用倒置数组的函数inversionArr(&arr);printf("倒置后数组元素为:\n");showArr(&arr);//调用冒泡排序的函数sortArr(&arr);printf("排序后的数组元素为:\n");showArr(&arr);getchar();return 0;}/* *初始化数组的函数 */void initArr(struct Array * pArr, int length){pArr->pBase = (int *)malloc(sizeof(int) * length );if(NULL == pArr->pBase)//如果内存分配失败{printf("动态内存分配失败!\n");exit(-1);//终止整个程序}else//如果内存分配成功,就初始化数组{pArr->len = length;pArr->cnt = 0;} return;            }/* *输出数组中的所有元素的函数 */void showArr(struct Array * pArr){if(isEmpty(pArr))//如果数组为空{printf("数组为空!\n");}else//如果数组不为空{int i;for(i=0;i<pArr->cnt;i++) printf("%d ", pArr->pBase[i]);printf("\n");}}/* *判断数组是否为空的函数 */bool isEmpty(struct Array * pArr){//判断元素是否为0if(0 == pArr->cnt)return true;elsereturn false;}/* *判断数组是否已满的函数 */bool isFull(struct Array * pArr){if(pArr->cnt == pArr->len)return true;elsereturn false;}/* *在数组末尾追加元素的函数 */bool appendElement(struct Array * pArr, int value){if(isFull(pArr))//满时返回falsereturn false;//不满时追加pArr->pBase[pArr->cnt] = value;pArr->cnt++;return true;}/* *向数组中插入元素的 */bool insertElement(struct Array * pArr, int position, int value){if(isFull(pArr))return false;if(position < 1 || position > pArr->cnt+1)return false;int i;for(i=pArr->cnt-1;i>=position-1;i--){pArr->pBase[i+1] = pArr->pBase[i];}//插入值pArr->pBase[position-1] = value;pArr->cnt++;return true;}/* *删除数组中指定位置处的元素 */bool deleteElement(struct Array * pArr, int position, int * pValue){int i;if(isEmpty(pArr))//数组为空不能删除return false;if(position < 1 || position > pArr->cnt)//要删除的元素的位置不合法return false;//将要删除的元素赋给pValue*pValue = pArr->pBase[position-1];//删除元素,并将后面的元素往前移for(i=position;i<pArr->cnt;i++){pArr->pBase[i-1] = pArr->pBase[i];}pArr->cnt--;return true;}/* *将数组元素倒置的函数 */void inversionArr(struct Array * pArr){int i = 0;int j = pArr->cnt-1;int t;while(i < j){t = pArr->pBase[i];pArr->pBase[i] = pArr->pBase[j];pArr->pBase[j] = t;i++;j--;}return;}/* *冒泡排序的函数 */void sortArr(struct Array * pArr){int i,j,t;for(i=0;i<pArr->cnt;i++){for(j=i+1;j<pArr->cnt;j++){if(pArr->pBase[i] > pArr->pBase[j]){t = pArr->pBase[i];pArr->pBase[i] = pArr->pBase[j];pArr->pBase[j] = t;}}}}/* *获取指定位置元素的函数,positon从1开始 */int getElement(struct Array * pArr, int position){return pArr->pBase[position - 1];}

原创粉丝点击