#include "stdafx.h" #include <malloc.h> #include <stdlib.h> struct Array { int * pBase; //存储的是数组第一个元素的地址 int len; //数组所能容纳的元素的最大个数 int cnt; //当前数组有效元素的个数 //int increment; //自动增长因子 }; void init_arr(struct Array * pArr, int length); bool append_arr(struct Array * pArr,int val); bool insert_arr(struct Array * pArr,int pos,int val); bool delete_arr(struct Array * pArr, int pos, int * pVal); bool is_empty(struct Array * pArr); bool is_full(struct Array * pArr); void show_arr(struct Array * pArr); void inversion_arr(struct Array * pArr); void sort_arr(struct Array * pArr); int main() { Array arr; //数组初始化 init_arr(&arr,10); //显示数组中的元素 show_arr(&arr); //向数组中追加元素 append_arr(&arr, 1); append_arr(&arr, 2); append_arr(&arr, 3); append_arr(&arr, 4); append_arr(&arr, 5); append_arr(&arr, 6); append_arr(&arr, 7); //显示数组中的元素 show_arr(&arr); //向数组中追加元素 insert_arr(&arr, 1, 99); insert_arr(&arr, 6, 55); //显示数组中的元素 show_arr(&arr); //删除一个元素 int q; delete_arr(&arr,1,&q); printf("删除的是:%d\n", q); //显示数组中的元素 show_arr(&arr); //倒置数组 inversion_arr(&arr); //显示数组中的元素 show_arr(&arr); //对数组进行排序 sort_arr(&arr); //显示数组中的元素 show_arr(&arr); getchar(); return 0; } void init_arr(struct Array * pArr,int length) { //pArr指针变量所指向的Array结构体变量中的pBase成员 pArr->pBase = (int *)malloc(sizeof(int) * length); //内存分配失败为NULL if (NULL == pArr->pBase) { printf("动态内存分配失败!\n"); exit(-1); } else { pArr->len = length; pArr->cnt = 0; } return; } bool is_empty(struct Array * pArr) { if (pArr->cnt == 0) { return true; } else { return false; } } bool is_full(struct Array * pArr) { if (pArr->cnt == pArr->len) { return true; } else { return false; } } void show_arr(struct Array * pArr){ if (is_empty(pArr)) { printf("数组为空!\n"); } else { for (int i = 0; i < pArr->cnt; i++) { printf("%d ",pArr->pBase[i]); } printf("\n"); } } bool append_arr(struct Array * pArr, int val) { if (is_full(pArr)) { printf("数组满了!\n"); return false; } else { pArr->pBase[pArr->cnt] = val; (pArr->cnt)++; return true; } } bool insert_arr(struct Array * pArr, int pos, int val) { //pos从1开始,如果插入的位置小于1或大于数组的有效个数,则插入失败 if (is_full(pArr)) { return false; } if (pos<1 || pos>pArr->cnt+1) { return false; } int i; //插入一个元素,需要从插入的位置开始,所有的元素向后移动 //先移动数组最后面的元素,i为需要移动的元素的下标,所以--i; for (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 Array * pArr, int pos, int * pVal) { if (is_empty(pArr)) { return false; } if (pos<1 || pos>pArr->cnt) { return false; } int i; //删除之前,先将要删除的元素赋值给pVal *pVal = 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 Array * pArr) { //借助变量t交换两个变量的值 /* 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; } */ //在没有第三个变量的前提下,交换两个变量的值。 int i = 0; int j = pArr->cnt - 1; while (i<j) { pArr->pBase[i] = pArr->pBase[i] + pArr->pBase[j]; pArr->pBase[j] = pArr->pBase[i] - pArr->pBase[j]; pArr->pBase[i] = pArr->pBase[i] - pArr->pBase[j]; ++i; --j; } return; } void sort_arr(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; } } } }
顺序表——数组:通过连续的存储单元来实现存储的数据集合
猜你喜欢
转载自blog.csdn.net/mypc2010/article/details/80183344
今日推荐
周排行