C语言简单模拟ArrayList
来源:互联网 发布:小米6数据连接上不了网 编辑:程序博客网 时间:2024/06/02 01:59
//// main.c// ArrayList//// Created by Adam on 15/4/7.// Copyright (c) 2015年 Bosch. All rights reserved.//#include <stdio.h>#include <stdlib.h>#include <stdbool.h>struct Array{ int * qarr; int len ; int total;};//初始化void initArray(struct Array * array,int length);//遍历数组void showArray(struct Array * array);//是否是空数组bool isEmpty(struct Array * array);//追加元素bool appendValue(struct Array * array,int val);//是否为满bool isFull(struct Array * array);//插入元素bool insertValue(struct Array * array,int pos,int val);//删除元素bool delecteValue(struct Array * array,int pos);//倒置数组void inversion(struct Array *array);//得到元素 传入下标bool getValue(struct Array * array,int index,int * val);//查找元素 传入值int findValue(struct Array * array,int val);int main(void){ struct Array arr; int val;
<pre name="code" class="cpp"> //测试初始化 initArray(&arr,10); //测试追加 appendValue(&arr,1); appendValue(&arr,5); appendValue(&arr,2); appendValue(&arr,7); appendValue(&arr,3); printf("追加后的数组为:"); showArray(&arr); printf("\n"); //测试插入 insertValue(&arr, 3, 44); printf("插入后的数组为:"); showArray(&arr); printf("\n"); //测试删除 deleteValue(&arr, 4); printf("删除后的数组为:"); showArray(&arr); printf("\n"); //测试倒置 inversion(&arr); printf("倒置后的数组为:"); showArray(&arr); printf("\n"); //测试get getValue(&arr,3,&val); printf("I get the number:%d\n",val); //测试find int index = findValue(&arr, 5); printf("I find the number:%d\n",index);
}void initArray(struct Array * array,int length){ array->qarr = (int *)malloc(sizeof(int) * length); if(NULL == array->qarr) { printf("分配内存失败"); exit(-1); } array->len = length; array->total = 0; return;}void showArray(struct Array * array){ if(isEmpty(array)) { printf("该数组是空的"); } else { for(int i=0;i<array->total;i++) { printf("%d \t",(array->qarr)[i]); } }}bool appendValue(struct Array * array,int val){ if (isFull(array)) { return false; } array->qarr[array->total]=val; array->total++; return true;}bool insertValue(struct Array * array,int pos,int val){ if(pos<1 || pos>array->total+1) { return false; } else if (isFull(array)) { return false; } for (int i=array->total-1; i>=pos-1; i--) { array->qarr[i+1] = array->qarr[i]; } array->qarr[pos-1] = val; array->total++; return true;}bool deleteValue(struct Array * array,int pos){ if (isEmpty(array)) { return false; } else if(pos<1 || pos>array->total) { return false; } for (int i=pos; i<=array->total-1; i++) { array->qarr[i-1] = array->qarr[i]; } array->total--; return true;}bool getValue(struct Array * array,int index ,int * val){ if (index<0 || index>array->total-1) { return false; } else { *val = array->qarr[index]; return true; }}bool isEmpty(struct Array * array){ if(array->total == 0) { return true; } else { return false; }}int findValue(struct Array * array,int value){ int index; for (int i=0; i<array->total; i++) { if (array->qarr[i]==value) { index=i; break; } if (i==array->total-1) { return -1; } } return index;}bool isFull(struct Array * array){ if (array->total==array->len) { return true; } else { return false; } }void inversion(struct Array * array){ int i=0; int j=array->total-1; int temp; while (i<j) { temp=array->qarr[i]; array->qarr[i]=array->qarr[j]; array->qarr[j]=temp; i++; j--; } }
0 0
- C语言简单模拟ArrayList
- 简单模拟ArrayList集合
- C语言模拟简单的计算器
- c语言简单小游戏(模拟魔塔)
- ArrayList(c语言)
- c语言 ---- ArrayList实现
- C#ArrayList简单应用
- C#ArrayList简单应用
- C#ArrayList简单应用
- 简单模拟ArrayList-迭代器设计模式
- 简单模拟ArrayList的一些方法
- 简单模拟ArrayList-迭代器设计模式
- 学生信息管理系统简单模拟(C语言实现)
- C语言大学期末考试模拟试卷 很简单
- 纯C语言简单模拟C++的虚函数表
- c语言模拟实现三次密码登录(简单)
- c语言编程 :模拟实现简单扫雷游戏
- 利用c语言来进行简单的模拟ATM机
- linux 下将ffmpeg源码目录列表转换存放到csv文件
- Android开发随记(2)-- onClick事件统一处理、系统界面跳转
- RHEL6下获取安装包(RPM)而不安装的方法
- 第27课时,自测
- 将数据成员改为私人的(第二章第二题)
- C语言简单模拟ArrayList
- c++变异问题 对‘std::ios_base::Init::Init()’未定义的引用
- 【Unity】数据持久化
- 雨滴效果
- java web项目启动时浏览器路径不用输入项目名称方法
- 研发流程中的产品测试
- Spinners
- UVa 514 数据结构栈
- 【erlang 002】erlang中的错误及异常处理