数据结构之重构数组
代码肯定有很多问题,没有仔细去完善,现在大致目的是弄清楚这些算法,现在贴出来供以后自己细看,也希望能帮助需要帮助的人
/*************************************************************************
> File Name: ArrayList.cpp > Author: Darin > Mail: dyy726@qq.com > Created Time: 2013年06月06日 星期四 07时57分32秒 ************************************************************************/ #include<stdio.h> #include<malloc.h> #include<string.h> typedef struct ArrayList { int *Base; int length; int count; }Array; bool iscorrect(int cnt,int length); //判断所输入的下标是否正确 void exchange(int *p1,int *p2);//交换函数给抽出来了 bool isfull(Array *array);//数组是否满了 bool isempty(Array *array); bool init(Array *array,int lenth);//初始化数组 bool add(Array *array,int num);//添加 bool insert(Array *array,int count,int num);//插入 bool deleteArrary(Array *array,int count);//删除 int get(Array *array,int count);//获取 bool sort(Array *array);//排序 void show(Array *array);//输出数组 int main() { Array array; init(&array,5); add(&array,1); add(&array,2); add(&array,3); add(&array,4); add(&array,5); insert(&array,5,10); show(&array); insert(&array,-1,20); insert(&array,1,20); show(&array); deleteArrary(&array,3); show(&array); sort(&array); show(&array); return 0; } bool iscorrect(int cnt,int length) { if(cnt<=0 ||cnt >=length) return false; return true; } void exchange(int *p1,int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; } bool isfull(Array *array) { if(array->count == array->length-1) { return true; } else return false; } bool isempty(Array *array) { if(array->count == -1 ) return true; else return false; } bool init(Array *array,int lenth) { //printf("Working\n"); array->Base = (int *)malloc(sizeof(int)*lenth); if (array->Base == NULL) { printf("Create failure"); return false; } array->length = lenth; array->count = -1; return true; } bool add(Array *array,int num) { //printf("Working\n"); if(!isfull(array)) { array->count++; array->Base[array->count] = num; return true; } else return false; } bool insert(Array *array,int count,int num) { int i =0; char y; bool yes = false; if(!iscorrect(count,array->length+1)) return false; if(!isfull(array)) { yes = true; } else { printf("The array if full,To add the length of the array ? y/n:"); gets(&y); if(y == 'Y'|| y == 'y'){ yes = true; array->length++; }else yes = false; } if (yes) { for (i =array->count;i>=count-1;i--){ exchange(&(array->Base[i+1]),&(array->Base[i])); } array->Base[count-1] = num; array->count++; return true; } else return false; } bool deleteArrary(Array *array,int count) { int i = 0; if(!iscorrect(count,array->length)) return false; if(isempty(array)) { printf("The array is empty"); return false; } else { for(i=count-1;i<array->count;i++) { exchange(&(array->Base[i]),&(array->Base[i+1])); } array->count--; array->length--; return true; } } int get(Array *array,int count) { if(!iscorrect(count,array->length)) return false; return array->Base[count-1]; } bool sort(Array *array) { int i=0; int j=0; if(isempty(array)) return false; for(i=0;i<array->count;i++){ for(j=0;j<array->count-i;j++) { if(array->Base[j]>array->Base[j+1]) { exchange(&(array->Base[j]),&(array->Base[j+1])); } } } return true; } void show(Array *array){ int i=0; if(isempty(array)) { printf("The Array is empty"); return; } for (i=0;i<=array->count;i++) printf("%d ",array->Base[i]); printf("\n"); }