//
// 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--;
}
}