C语言中动态数组操作实践
2013-07-23 12:54
316 查看
#include<stdio.h> #include<malloc.h> //包含了malloc函数 #include<stdlib.h> //包含了exit函数 //定义了一个数据类型,该数据类型的名字为struct Array struct 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); } else printf("删除失败!\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) { //判断元素是否为0 if(0 == pArr->cnt) return true; else return false; } /* *判断数组是否已满的函数 */ bool isFull(struct Array * pArr) { if(pArr->cnt == pArr->len) return true; else return false; } /* *在数组末尾追加元素的函数 */ bool appendElement(struct Array * pArr, int value) { if(isFull(pArr)) //满时返回false return 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]; }
相关文章推荐
- C语言提高-39讲: 动态存储管理与动态数组的实现(实践)
- 《C语言及程序设计》实践参考——为动态数组扩容
- 《C语言及程序设计》实践项目——动态数组 ->【项目2-动态数组排序】
- 《C语言及程序设计》实践项目——动态数组 ->【项目1-学生人数没个准】
- 《C语言及程序设计》实践项目——动态数组 ->【项目3-为动态数组扩容】
- C语言实现动态数组
- C语言实现动态数组大小
- 程序设计基石与实践之C语言指针和数组基础
- C语言中动态的申请二(三)及多维数组的使用方法及释放方法
- C语言声明一维动态数组
- jq实现最多复选框,js数组操作实践。
- jquery 实践操作:div 动态嵌套(追加) div
- C语言小程序(六)、数组操作
- c语言实现一个简单的通用动态数组
- C语言建立动态数组
- 复习C语言系列二:动态调用函数指针数组
- C语言中动态数组的实现
- C语言:数组的初始化及动态申请
- C语言的那些小秘密之动态数组
- C语言完美实现动态数组代码分享