18 数组基本操作(未完成)
标签:i++ show ber amp version void color memory struct
#include
#include
#include string.h> //字符函数库,这里不需要
#include //exit 函数需要
struct arr
{
char* arr_name;
int len; //当前数组长度
int maxlen;
};
void init_arr(struct arr* array, int length);
void show_arr(struct arr* array);
bool is_empty(struct arr* array);
bool is_full(struct arr* array);
int get_arr(struct arr* array, int num); //获得小标下标为num 的数组内容
bool append_arr(struct arr* array, int num); //数组尾部追加一个元素
bool insert_arr(struct arr* arr, int x, int num); //x 为插入的数,num为插入为值的下标
bool delete_arr_num(struct arr* array, int num);
bool delete_arr_x(struct arr* array, int x);
void sort_arr(struct arr* array);
void inversion_arrr(struct arr* array); // 倒叙排列
main()
{
struct arr array;
init_arr(&array, 10);
append_arr(&array, 1);
append_arr(&array, 2);
append_arr(&array, 3);
insert_arr(&array, 4, 3);
delete_arr_num(&array, 1);
show_arr(&array);
printf("\n");
//get_arr(&array, 0);
}
//参数length 用户指定的数组长度
void init_arr(struct arr* ar, int length) {
ar->arr_name = malloc(sizeof(int));
if (ar->arr_name == NULL)
{
printf(" malloc memory error!!");
exit(-1);
}
else {
ar->maxlen = length;
ar->len = 0;
}
}
void show_arr(struct arr* ar) {
int i;
printf("array lenth is %d \n", ar->len);
printf("array: ");
if (is_empty(ar)) {
printf(" array is empty!!");
}
else
for (i = 0; i len; i++)
printf("%d ", ar->arr_name[i]);
}
bool is_empty(struct arr* ar)
{
if (ar->len == 0)
return true;
else
return false;
}
bool is_full(struct arr* ar) {
if (ar->len == ar->maxlen)
return true;
else
return false;
}
//x 为增加的数组元素值
bool append_arr(struct arr* ar, int x) {
bool app = false;
if (is_full(ar))
{
printf("array is full !");
}
else {
ar->arr_name[ar->len] = x;
ar->len++;
app = true;
}
return app;
}
int get_arr(struct arr* arr, int num) {
if (num > arr->len)
printf("the number is too big!");
else
return arr->arr_name[num];
//printf("array[%d] = %d ", num, arr->arr_name[num]);
}
bool insert_arr(struct arr* arr, int x, int num) {
bool ins = true;
int i;
if (num > arr->len) {
ins = false;
printf("number is too large !\n");
}
else if (num == (arr->len)) {
arr->arr_name[arr->len] = x;
arr->len += 1;
}
else {
for (i = (arr->len); i >= num; i--)
arr->arr_name[i] = arr->arr_name[i - 1];
arr->len += 1;
arr->arr_name[num] = x;
}
return ins;
}
bool delete_arr_num(struct arr* arr, int num) {
int i;
bool del = true;
if (is_empty(arr)) {
printf("array is empty!");
del = false;
}
else if (num > arr->len) {
printf("number is too large !\n");
del = false;
}
else if (num = arr->len) {
arr->len -= 1;
}
else
for (i=num; i len-num; i++)
{
arr->arr_name[i] = arr->arr_name[i + 1];
arr->len -= 1;
}
}
//总
18 数组基本操作(未完成)
标签:i++ show ber amp version void color memory struct
原文地址:https://www.cnblogs.com/abel2020/p/13022419.html
评论