p12-连续存储数组的算法演示

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>//包含了exit 
//定义了一个数据类型,该数据类型的名字叫做struct Array, 该数据类型含有三个成员,分别是lenght, data, size
struct Array {
	int lenght;数组所能容纳的最大元素的个数
	int* data;//分配到的动态数组首元素的地址
	int size;//当前数组有效元素的个数
};
void init_Array(struct Array*pst,int len) {
	//动态内存分配能在程序运行过程中发现内存分配不够时可以做出调整 
	pst->data = (int*)malloc(sizeof(int) * len);//可以与a[len] = {1,3,4,5...}类似 不过其是固定运行过程中不可改变, pst等价与 a[len]中的a,都是指向数组的首地址 *pst等价与a[0] ,pst与a都是数组元素首地址,且相等
	//bug 当时我写错了 pst->data = (struct Array*)malloc(sizeof(int)*len)
	if (pst->data == NULL) {
		printf("分配动态内存失败");
		exit(-1);
	}
	else {
		printf("动态内存分配成功");//pst分配到数组的首地址占四个字节
		pst-> lenght = len;
		pst-> size = 0; //因为只是分配了数组内存未插入元素
		return;
	}
}
bool is_full(struct Array*p) {
	if (p->size == p->lenght) {
		printf("数组是满的");
		return true;
	}
	
		return false;
	
}
bool delete_Array(struct Array* p, int pos, int* numeber) {
	if (pos<0 || pos>p->size) {
		printf("删除失败");
		return false;
	}
	*numeber = p->data[pos - 1];
	for (int i = pos; i < p->size; i++) {
		p->data[i - 1] = p->data[i];
	}
	p->size--;
	
	return true;
}
bool is_empty(struct Array* p) {
	if (0 == p->size) {
		printf("数组为空");
		return true;
	}
	else {
		return false;
	}
}
bool append_Array(struct Array*p,int numeber) {
	if (is_full(p)){
		printf("追加失败");
		return false;
	}
	else {
		p->data[p->size] = numeber;
		p->size++;
			return true;
	}
		
	}
void shor_Array(struct Array*p) {
	for (int i = 0; i < p->size - 1; i++) {
		for (int j = 0; j < p->size - 1 - i; j++) {
			if (p->data[j] > p->data[j + 1]) {
				int temp;
				temp = p->data[j];
				p->data[j] = p->data[j + 1];
				p->data[j + 1] = temp;
			}
		}
	}
}


int show_Array(struct Array* p) {
	if (is_empty(p)) {
		printf("数组为空打印失败");
		return 0;
	}
	else {
		for (int i = 0; i< p->size; i++) {
			printf("%d", p->data[i]);
		}
	}
}
bool intsert_Array(struct Array* p,int pos, int numeber) {

	if (is_full(p)) {
		printf("满了不能插入了");
		return false;
	}
	if (pos<1 || pos>p->size + 1) {
		printf("位置没有负数,且不能大于有效个数的前一位,不然插入的话就不是连续的数组了");
	}
	for (int i = p->size - 1; i >= pos - 1; i--) {
		p->data[i + 1] = p->data[i];
	}
	p->data[pos - 1] = numeber;
	p->size++;
	
	return true;
}
void intversion_Array(struct Array*p) {
	int i = 0;
	//int j = p->data[p->size - 1]; bug:这样写会导致指针越界
	int j = p->size - 1;
	int temp;
	while (i < j) {
		temp = p->data[i];
		p->data[i] = p->data[j]; 
		
		p->data[j] = temp;
		i++;
		j--;
	}
	return;
	
	
 }


int main() {
	int numeber;
	struct Array arr;
	init_Array(&arr,5);
	printf("\n");
	append_Array(&arr,4); //arr等价于*p  其中*p是指向对象的值      &arr等价于p都是地址

	append_Array(&arr,8);

	append_Array(&arr,6);

	append_Array(&arr,7);

	show_Array(&arr);
	printf("\n");

	printf("插入的数是:99");
	printf("\n");
	printf("插入后的数:");
	intsert_Array(&arr,3,99);
	show_Array(&arr);

	if (delete_Array(&arr, 4, &numeber)) {
		printf("删除成功删除的元素是:%d\n", numeber);
	}
	else {
		printf("删除失败");
	}
	printf("删除后的数组:");
	show_Array(&arr);

	intversion_Array(&arr);

	printf("倒置后的数组:");
	show_Array(&arr);
	printf("\n");

	printf("排序后的数组:");
	shor_Array(&arr);
	show_Array(&arr);



	return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨启飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值