ArrayList生成(用C语言)

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
void init(struct Array * pArray,int length);
void traverse(struct Array * pArray);
bool is_empty(struct Array * pArray);
bool is_full(struct Array * pArray);
bool add(struct Array * pArray,int val);
bool insert_before(struct Array * pArray,int pos,int val);
bool delete_arr(struct Array * pArray,int pos,int * val);
void sort(struct Array * pArray);
struct Array{
	int * pBase; //存储数组的第一个元素的地址 
	int length; //数组元素的长度 
	int count; //数组元素的个数 
};
int main(void){
	struct Array array; 
	init(&array,10); 
	add(&array,-2);
	add(&array,2);
	add(&array,1);
	add(&array,8);
	add(&array,5);
	add(&array,6);
	insert_before(&array,5,7);
//	insert_before(&array,7,8);
	//sort(&array);
//  int val;
//	bool flag=	delete_arr(&array,6,&val);
//    if(flag){
//    	printf("删除成功,删除的元素是%d\n",val);
//	}
	traverse(&array);
	sort(&array);
	printf("\n");
	traverse(&array);
	return 0;
}

void init(struct Array * pArray,int length){
	pArray->pBase=(int *)malloc(sizeof(int *)*length); //此处特别注意,此时数组里面存储的是int 类型的元素,不是struct Array类型 
	pArray->length=length;
	pArray->count=0;
	//printf("%d ,%d ",pArray->length,pArray->count);
} 
void traverse(struct Array * pArray){

	if(is_empty(pArray)){
		printf("数组元素为空\n");
	}else{
	int i;
		for(i=0;i<pArray->count;i++){
			printf("%d ,",pArray->pBase[i]); 
		}
	}
	return;
}
bool is_empty(struct Array * pArray){
	if(0==pArray->count){
		return true;
	}
	return false;
}
bool is_full(struct Array * pArray){
	if(pArray->length==pArray->count){
		printf("数组元素已满,添加失败\n");
		return true;
	}
	return false;
}
bool add(struct Array * pArray,int val){ //增加元素 
	if(is_full(pArray)){
		return false;
	}
	else{
		pArray->pBase[pArray->count]=val; //假设 有 3个元素,此时count=3,新加入的元素下标刚好是3;
			//pArray->pBase[0]=val;						//假设有0个元素,此时count=0,下标刚好是0; 
		(pArray->count)++; // 元素个数加1; 
		return true;
	}
}
/*
1     2    3     4    5   
a[0] a[1]  a[2]  a[3] a[4]  a[5]
count =5 
pos =3
int i=count;i>
a[5]=a[4]
a[4]=a[3]
a[3]=a[2]

for(i=count;i>=pos;i--){
	a[i]=a[i-1];
}
a[pos-1]=val;
count++;
------------------------
pos =6;
count=5;
for不执行,直接插入;


*/
bool insert_before(struct Array * pArray,int pos,int val){ //在指定位置的前面插入元素 
	if(is_full(pArray)){
		return false;
	}
	if(pos<1||pos>pArray->count+1){
		return false;
	} else{
		int i;
		for(i=pArray->count;i>=pos;i--){
			pArray->pBase[i]=pArray->pBase[i-1]; 
		}
		pArray->pBase[pos-1]=val;
		pArray->count++; //注意个数要加1 
		return true;
	}
}
bool delete_arr(struct Array * pArray,int pos,int * val){
	if(pos<1||pos>pArray->count){
		return false;
	}else{
		*val=pArray->pBase[pos-1];
		int i;
		for(i=pos;i<pArray->count;i++){
			pArray->pBase[i-1]=pArray->pBase[i];
		}
		pArray->count--;
		return true;
	}
}
 void sort(struct Array * pArray){ //冒泡排序 
 	int i,j;
 	for(i=0;i<pArray->count-1;i++){ //具体参看下面的注释 
 		for(j=i+1;j<pArray->count;j++){
 			if(	pArray->pBase[i]>pArray->pBase[j]){
 				int t;
 				t=pArray->pBase[i];
 				pArray->pBase[i]=pArray->pBase[j];
 				pArray->pBase[j]=t;
			 }
		 }
	 }
 	return ;
 }



 
	排序的详细过程
	-7 8 2 6 3 9  
i=0 j循环 j从1到5
 第一次  -7 8
 第二次  -7 2  
 第三次  -7 6
 第四次  -7 3
 第五次  -7 9   
 -7 8 2 6 3 9  
 -----------------
 i=1 j循环 j从2到5
 第六次   8 2 
				换 -7 2 8 6 3 9
 第七次   2 6   
 第八次	  2 3
 第九次   2 9
 -7 2 8 6 3 9
 ----------------------------
 i=2 j循环 j从2到5
 第10次   8  6  换 -7 2 6 8 3 9
 第11次   6  3  换 -7 2 3 8 6 9
 第12次   3  9
 -7 2 3 8 6 9
 ---------------------------
 i=3 j循环 j从3到5
 第13次   8  6 换 -7 2 3 6 8 9
 第14次   6  9 
-7 2 3 6 8 9
 ----------------------------
 i=4 j循环 j从4 到5
 第15次  8 和 9
 
  -7 8 2 6 3 9  
  -7 2 8 6 3 9
  -7 2 3 8 6 9
  -7 2 3 6 8 9
  -7 2 3 6 8 9 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值