数据结构笔记(3)数组的算法
来源:互联网 发布:uusee网络电视手机版 编辑:程序博客网 时间:2024/05/24 11:15
连续存储 数组的算法演示
#include <stdio.h>#include <stdlib.h>#include <malloc.h>struct Arr{int * pBase;//存储数组第一个元素的地址int len;//数组所能容纳的最大元素的个数int cnt;//当前数组有效元素的个数};void intail_arr(struct Arr * pArr,int length);void show_arr(struct Arr * pArr);void inversion_arr(struct Arr * pArr);void sort_arr(struct Arr * pArr);bool is_empty(struct Arr * pArr);bool is_full(struct Arr * pArr);bool append_arr(struct Arr * pArr,int val);bool insert_arr(struct Arr * pArr,int pos,int val);bool delete_arr(struct Arr * pArr,int pos,int* pDeleteVal);int main(void){int deleteVal;struct Arr arr;intail_arr(&arr,6);printf("向数组追加几个元素\n");append_arr(&arr,1);append_arr(&arr,-2);append_arr(&arr,10);append_arr(&arr,8);append_arr(&arr,5);show_arr(&arr);printf("在位置3处插入一个元素\n");insert_arr(&arr,3,33);show_arr(&arr);printf("删除位置3处的元素\n");if(delete_arr(&arr,3,&deleteVal)){printf("你删除的元素是:%d\n",deleteVal);}else{printf("删除失败!");}show_arr(&arr);printf("倒置数组中的元素\n");inversion_arr(&arr);show_arr(&arr);printf("给数组元素按升序排序\n");sort_arr(&arr);show_arr(&arr);system("pause");return 0;/*屏幕输出向数组追加几个元素1 -2 10 8 5在位置3处插入一个元素1 -2 33 10 8 5删除位置3处的元素你删除的元素是:331 -2 10 8 5倒置数组中的元素5 8 10 -2 1给数组元素按升序排序-2 1 5 8 10请按任意键继续. . .*/}//初始化数组void intail_arr(struct Arr * pArr,int length){pArr->pBase= (int *)malloc(sizeof(int)*length);if(pArr->pBase==NULL){printf("动态分配内存失败!\n");exit(-1);//终止整个程序}else{pArr->len=6;pArr->cnt=0;}return;}//判断数组是否为空bool is_empty(struct Arr * pArr){if(pArr->cnt==0)return true;elsereturn false;}//输出数组void show_arr(struct Arr * pArr){if(is_empty(pArr)){printf("数组为空!\n");}else{for(int i=0;i<pArr->cnt;i++){printf("%d ",pArr->pBase[i]);}printf("\n");}}//判断数组是否满了bool is_full(struct Arr * pArr){if(pArr->cnt==pArr->len)return true;elsereturn false;}//向数组追加元素bool append_arr(struct Arr * pArr,int val){if(is_full(pArr))return false;else//如果数组不满可以追加元素{pArr->pBase[pArr->cnt]=val;pArr->cnt++;return true;}}//向数组的某个位置插入元素(pos从1开始)bool insert_arr(struct Arr * pArr,int pos,int val){if(is_full(pArr))return false;if(pos<1||pos>pArr->cnt+1)return false;for(int i=pArr->cnt-1;i>=pos-1;i--){pArr->pBase[i+1]=pArr->pBase[i];}pArr->pBase[pos-1]=val;pArr->cnt++;return true;}//删除某个位置的元素bool delete_arr(struct Arr * pArr,int pos,int * pDeleteVal){if(is_empty(pArr))return false;if(pos<1||pos>pArr->cnt)return false;int i;*pDeleteVal=pArr->pBase[pos-1];for(i=pos;i<pArr->cnt;i++){pArr->pBase[i-1]=pArr->pBase[i];}pArr->cnt--;return true;}//倒置数组元素void inversion_arr(struct Arr * 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 sort_arr(struct Arr * 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;}}}return;}
- 数据结构笔记(3)数组的算法
- 数据结构与基本算法的学习笔记-数组-3
- 数据结构与算法分析笔记与总结(java实现)--数组2:旋转数组的最小数字
- 数据结构与算法分析笔记与总结(java实现)--数组8:数组中重复的数字
- 我的数据结构、算法笔记
- 【郝斌数据结构自学笔记】12-13_连续存储数组的算法演示
- 《数据结构与算法》学习笔记4 存储对象的数组和大O表示法
- 《算法导论》学习笔记之Chapter10基本数据结构---栈的数组实现
- 算法-一种数组环形队列的数据结构
- 数据结构和算法--栈的数组实现
- 数据结构和算法--队列的数组实现
- 数据结构与算法分析笔记与总结(java实现)--数组4:数组中出现次数超过一半的数字
- 数据结构与算法分析笔记与总结(java实现)--数组5:连续子数组的最大和
- 数据结构与算法分析笔记与总结(java实现)--数组6:把数组排成最小的数
- 数据结构与算法分析笔记与总结(java实现)--数组9:数字在排序数组中出现的次数
- 数据结构与算法分析笔记与总结(java实现)--数组10:数组中只出现一次的数字
- 《数据结构与算法》学习笔记3 有序数组与二分查找
- 《数据结构与算法C#语言描述》笔记2_数组
- socket编程实例
- Android on OMAP
- androdi draw9patch 图片使用中的小问题
- 理解Session的上下文
- AppDomain.CurrentDomain.BaseDirectory
- 数据结构笔记(3)数组的算法
- 时评:考上公务员一定要靠关系吗?
- SVN创建及合并分支
- 常用英语口语句型100句
- [终结] T00ls Lpk Sethc v3.0 正式版
- 英文连接词
- HTTPLook 注册码
- T00ls Lpk Sethc 首创lpk劫持方式粘滞键后门,体积超小!
- T00ls Lpk Sethc 全新v2.0版(附LPK提权)