线性结构之数组[基于郝斌课程]
线性结构:把所有的结点用一根线穿起来连续存储[数组]
什么叫做数组:元素类型相同,大小相等
/*
@file main.c
@brief 线性结构之数组
@author EricsT (EricsT@163.com)
@version v1.0.0
@date 2025-09-21
@history 2025-09-21 EricsT - 新建文件
*/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct Arr//定义了一个复合数据类型
{
int* ptrBase;//存储数组的第一个元素的地址
int len;//数组所能容纳的最大元素的个数
int cnt;//当前数组有效元素的个数
//int increment;//自动增长因子
};
void init_arr(Arr* ptrArr, const int len);//初始化
bool append_arr(Arr* ptrArr, int appendValue);//追加
bool insert_arr(Arr* ptrAee, int insertPos, int insertValue);//插入
bool delete_arr(Arr* ptrArr, int deletePos);//删除
int get_arr(Arr* ptrArr, int getPos);//获取某一元素
bool is_empty_arr(const Arr* ptrArr);//是否为空
bool is_full_arr(const Arr* ptrArr);//是否满
void sort_arr(Arr* ptrArr);//排序
void show_arr(const Arr* ptrArr);//显示
void inversion_arr(Arr* ptrArr);//倒置
int main(void)
{
Arr arr;//此时已经分配了一块内存,该内存大小为 sizeof(Arr)//此时只定义未初始化
init_arr(&arr, 6);
append_arr(&arr, 2);
append_arr(&arr, 3);
append_arr(&arr, 4);
append_arr(&arr, 5);
insert_arr(&arr, 2, 9);
show_arr(&arr);
printf("\n");
delete_arr(&arr, 3);
show_arr(&arr);
printf("\n");
printf("%d\n", get_arr(&arr, 3));
inversion_arr(&arr);
show_arr(&arr);
sort_arr(&arr);
printf("\n");
show_arr(&arr);
return 0;
}
void init_arr(Arr* ptrArr, const int len)
{
ptrArr->ptrBase = (int*)malloc(sizeof(int) * len);
if (NULL == ptrArr->ptrBase)
{
printf("动态内存分配失败\n");
exit(-1);//终止整个程序
}
else
{
ptrArr->len = len;
ptrArr->cnt = 0;
}
return;
}
void show_arr(const Arr* ptrArr)
{
if (is_empty_arr(ptrArr))
{
printf("该数组为空\n");
return;
}
for (int i = 0; i < ptrArr->cnt; ++i)
printf("%d ", *(ptrArr->ptrBase + i));
return;
}
bool is_empty_arr(const Arr* ptrArr)
{
if (0 == ptrArr->cnt)
return true;
return false;
}
bool append_arr(Arr* ptrArr, int appendValue)
{
if (is_full_arr(ptrArr))//满了
return false;
*(ptrArr->ptrBase + ptrArr->cnt) = appendValue;
ptrArr->cnt += 1;
return true;
}
bool is_full_arr(const Arr* ptrArr)
{
if (ptrArr->cnt == ptrArr->len)
return true;
return false;
}
bool insert_arr(Arr* ptrArr, int insertPos, int insertValue)
{
if (is_full_arr(ptrArr))
return false;
if (insertPos < 1)
return 0;
if (insertPos > ptrArr->len)
return false;
if (insertPos > ptrArr->cnt + 1)
return false;
for (int i = ptrArr->cnt - 1; i >= insertPos - 1; --i)
*(ptrArr->ptrBase + i + 1) = *(ptrArr->ptrBase + i);
*(ptrArr->ptrBase + insertPos - 1) = insertValue;
ptrArr->cnt += 1;
return true;
}
bool delete_arr(Arr* ptrArr, int deletePos)
{
if (is_empty_arr(ptrArr))
return false;
if (deletePos < 1)
return false;
if (deletePos > ptrArr->cnt)
return false;
for (int i = deletePos - 1; i < ptrArr->cnt - 1; ++i)
*(ptrArr->ptrBase + i) = *(ptrArr->ptrBase + i + 1);
ptrArr->cnt -= 1;
return true;
}
int get_arr(Arr* ptrArr, int getPos)
{
if (is_empty_arr(ptrArr))
return -1;
if (getPos < 1)
return -1;
if (getPos > ptrArr->cnt)
return -1;
return ptrArr->ptrBase;
}
//void inversion_arr(Arr* ptrArr)
//{
// int* ptr = (int*)malloc(sizeof(int) * ptrArr->cnt);
//
// for (int i = 0; i < ptrArr->cnt; ++i)
// ptr = ptrArr->ptrBase;
//
// for (int i = 0; i < ptrArr->cnt; ++i)
// ptrArr->ptrBase = ptr;
//
// free(ptr);
//}
void inversion_arr(Arr* ptrArr)
{
int i = 0;
int j = ptrArr->cnt - 1;
int temp = 0;
while (i < j)
{
temp = ptrArr->ptrBase;
ptrArr->ptrBase = *(ptrArr->ptrBase + j);
ptrArr->ptrBase = temp;
i++;
j--;
}
}
void sort_arr(Arr* ptrArr)
{
for (int j = 0; j < ptrArr->cnt; ++j)
{
for (int i = j + 1; i < ptrArr->cnt; ++i)
{
if (ptrArr->ptrBase > ptrArr->ptrBase)
{
int temp = ptrArr->ptrBase;
ptrArr->ptrBase = ptrArr->ptrBase;
ptrArr->ptrBase = temp;
}
}
}
}
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]