#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<stdbool.h>
//定义了一个数据类型,名叫struct Arr
struct Arr
{
int *pBase; //存储的是数组第一个元素的地址
int len; //数组的长度
int cnt; //当前数组的有效元素的个数
};
void init_arr(struct Arr *,int len); //初始化数组
void show_arr(struct Arr *); //显示数组
bool is_empty(struct Arr *); //判断数组是否为空
bool is_full(struct Arr *); //判断数组是否已满
bool append_arr(struct Arr *, int value); //向数组中追加value
bool insert_arr(struct Arr *, int pos, int value); //将value插入到数组中第pos个位置
bool delete_arr(struct Arr *, int pos); //删除数组中第pos个元素
int get_arr(struct Arr *, int pos); //获取第pos个元素
void inversion_arr(struct Arr *); //倒置元素
void sort_arr(struct Arr *); //对元素排序(有点忘记随便写的一个)
int main()
{
struct Arr array;
init_arr(&array,10);
append_arr(&array, 1);
append_arr(&array, 232);
append_arr(&array, 3);
insert_arr(&array, 3, 56);
append_arr(&array, 3453);
append_arr(&array, 32543);
append_arr(&array, 32);
append_arr(&array, 29);
delete_arr(&array, 5);
//printf("第%d个元素为%d\n\n\n",5 ,get_arr(&array, 5));
show_arr(&array);
sort_arr(&array);
show_arr(&array);
inversion_arr(&array);
show_arr(&array);
return 0;
}
void init_arr(struct Arr *pArr, int len)
{
pArr->pBase = (int*) malloc(sizeof(int) * len);
if (pArr->pBase == NULL)
{
printf("内存分配失败\n");
exit(0); //终止程序
}
else
{
pArr->len = len;
pArr->cnt = 0;
printf("初始化成功!\n");
}
return;
}
void show_arr(struct Arr *pArr)
{
if (is_empty(pArr)) //如果数组为空则提示用户
{
printf("数组内容为空!!!\n");
}
else
{
printf("数组内容:");
for (int i = 0; i < pArr->cnt; ++i)
{
printf("%d ", pArr->pBase[i]);
}
printf("\n");
// printf("数组长度:%d\n",pArr->len);
// printf("数组有效元素个数:%d\n", pArr->cnt);
}
}
bool is_empty(struct Arr *pArr)
{
if (pArr->cnt == 0)
return true;
else
return false;
}
bool is_full(struct Arr *pArr)
{
if (pArr->cnt == pArr->len) //拿数组中的有效元素个数和数组的实际长度比较即可的出数组时候已满
{
return true;
}
else
{
return false;
}
}
bool append_arr(struct Arr *pArr, int value)
{
if (is_full(pArr))
{
printf("数组已满无法添加%d\n",value);
return false;
}
pArr->pBase[pArr->cnt] = value; //数组向数组中添加value
(pArr->cnt)++; //更新数组的有效元素个数
return true;
}
bool insert_arr(struct Arr *pArr, int pos, int value)
{
int count = pArr->cnt;
if (pos > pArr->cnt || pos <= 0)
{
printf("插入位置错误!!!\n");
exit(0);
}
if (is_full(pArr)) //数组已满时
{
printf("插入%d失败,数组已满!!!\n",value);
return false;
}
if (pArr->cnt == pos) //插入位置刚好是最后一位时
{
append_arr(pArr, value);
// printf("直接追加元素已运行!!!\n\n\n"); //测试
return true;
}
while (count >= pos-1) //从后向前移动元素,当标志位等于要插入的元素位置时停止
{
pArr->pBase[count] = pArr->pBase[count-1];
--count;
}
pArr->pBase[pos-1] = value; //将元素插入到数组中第pos个位置
pArr->cnt++; //添加成功后更新将数组有效元素的个数
return true;
}
bool delete_arr(struct Arr *pArr, int pos)
{
if (pos > pArr->cnt || pos <= 0)
{
printf("删除位置错误!!!\n");
exit(0);
}
if (is_empty(pArr)) //数组已满时
{
printf("删除失败,数组为空");
return false;
}
while (pos != pArr->cnt) //从前向后移动元素,当标志位等于要删除元素的位置时停止
{
pArr->pBase[pos - 1] = pArr->pBase[pos];
pos++;
}
pArr->cnt--; //添加成功后更新将数组有效元素的个数
printf("删除元素成功!\n");
return true;
}
int get_arr(struct Arr *pArr, int pos)
{
if ((pos > pArr->cnt || pos <= 0) || is_empty(pArr))
{
printf("元素不存在\n");
exit(0);
}
return pArr->pBase[pos-1];
}
void inversion_arr(struct Arr *pArr)
{
if (is_empty(pArr))
{
printf("数组为空!!!\n");
exit(0);
}
if (pArr->cnt == 1)
{
show_arr(pArr);
return;
}
int temp; //交换的中介
for (int times = 1; times <=(pArr->cnt / 2) ; times++)
{
temp = pArr->pBase[times - 1];
pArr->pBase[times - 1] = pArr->pBase[pArr->cnt - times];
pArr->pBase[pArr->cnt - times] = temp;
}
printf("倒置数组成功!\n");
return;
}
void sort_arr(struct Arr *pArr)
{
if (is_empty(pArr))
{
printf("数组为空!!!\n");
exit(0);
}
if (pArr->cnt == 1)
{
show_arr(pArr);
return;
}
int temp;
for (int i = 0; i < pArr->cnt; i++)
{
for (int j = i+1; j < pArr->cnt; j++)
{
if (pArr->pBase[i] > pArr->pBase[j])
{
temp = pArr->pBase[i];
pArr->pBase[i] = pArr->pBase[j];
pArr->pBase[j] = temp;
}
}
}
printf("排序数组成功!\n");
return;
}
专题一之顺序表:对数组的增、删、查、取、排序以及倒置操作
最新推荐文章于 2024-08-09 16:47:28 发布