对线性结构---数组的各种操作

#include<stdio.h>
#include<malloc.h>	//包含了malloc函数 
#include<stdlib.h>	//包含了exit函数 

//定义了一个数据类型,该数据类型的名字叫做struct Arr,该数据类型含有三个成员,分别是
//pBase,len,cnt 
struct Arr
{
	int * pBase;	//存储的是数组第一个元素的地址 
	int len;	//数组所能容纳的最大元素的个数 
	int cnt;	//当前数组有效元素的个数 
};
void init_arr(struct Arr * pArr,int length);	//创建一个数组,采用动态分配的方式 
bool is_empty(struct Arr * pArr);   //判断数组是否为空 
bool is_full(struct Arr * pArr);    //判断数组是否满了 
void show_arr(struct Arr * pArr);   //显示数组中的元素 
bool append_arr(struct Arr * pArr,int val);	   //在数组尾部追加元素 
bool insert_arr(struct Arr * pArr,int pos,int val);   //在数组中插入元素 
bool delete_arr(struct Arr * pArr,int pos,int * pVal);	//删除数组中的某个位置的元素, 
														//pos的值是从1开始
int get_arr(struct Arr * pArr,int pos);	  //获取某个位置的元素  
bool replase_arr(struct Arr * pArr,int pos,int val);	//将数组中某个位置的值替换成
														//指定的值,pos从1开始 
void inversion_arr(struct Arr * pArr);	//将数组元素倒置 
void sort_arr(struct Arr * pArr);	//给数组的元素排序,采用了冒泡排序法 
bool contains_arr(struct Arr * pArr,int val);	//判断数组中是否有某个元素,并输出元素的位置 
bool clear_arr(struct Arr * pArr);	//清空数组元素 


int main()
{
	struct Arr arr;
	int length;
	int del;
	int get;
	printf("请输入数组的长度:");
	scanf("%d",&length); 
	init_arr(&arr,length);
	append_arr(&arr,5);
	append_arr(&arr,2);
	append_arr(&arr,5);
	append_arr(&arr,3);
	append_arr(&arr,0);
	append_arr(&arr,52);
	append_arr(&arr,5);
	printf("数组中的元素为:\n"); 
	show_arr(&arr);
	get=get_arr(&arr,3);
	printf("第3个位置的元素是:%d\n",get);
	printf("在数组的第三个位置插入元素100\n");
	insert_arr(&arr,3,100);
	show_arr(&arr);
	printf("删除数组第4个位置的元素\n");
	if(delete_arr(&arr,4,&del))
	{
		printf("删除成功!\n");
		printf("您删除的元素是:%d\n",del);
	}
	else
	{
		printf("删除失败!\n");
	}
	show_arr(&arr);
	printf("将数组元素倒置\n");
	inversion_arr(&arr);
	show_arr(&arr);
	printf("将数组中第2个位置的元素替换成66\n");
	replase_arr(&arr,2,66);
	show_arr(&arr);
	printf("将数组中的元素排序\n");
	sort_arr(&arr);
	show_arr(&arr);
	printf("判断数组中是否有某个元素5,并输出元素的位置\n");
	contains_arr(&arr,5);
	show_arr(&arr);
	printf("清空数组\n");
	clear_arr(&arr);
	show_arr(&arr);
	return 0;
}

void init_arr(struct Arr * pArr,int length)
{
	pArr->pBase=(int *)malloc(sizeof(int)*length);
	if(NULL==pArr->pBase)
	{
		printf("动态内存分配失败!\n");
		exit(-1);	//终止整个程序 
	}
	else
	{
		pArr->len=length;
		pArr->cnt=0;
	}
	return ;
}
bool is_empty(struct Arr * pArr)
{
	if(0==pArr->cnt)
		return true;
	else
		return false;
}
bool is_full(struct Arr * pArr)
{
	if(pArr->cnt==pArr->len)
		return true;
	else
		return false;
}
void show_arr(struct Arr * pArr)
{
	int i; 
	if(is_empty(pArr))
	{
		printf("数组为空!\n");
	}
	else
	{
		for(i=0;i<pArr->cnt;i++)
			printf("%d ",pArr->pBase[i]);
		printf("\n");
	}
}
bool append_arr(struct Arr * pArr,int val)
{
	//数组满了的时候返回false 
	if(is_full(pArr))
	{
		printf("数组已满,不能再追加元素了!\n"); 
		return false;
	}	
	//不满时追加元素 
	pArr->pBase[pArr->cnt]=val;
	pArr->cnt++;
	return true;
}
bool insert_arr(struct Arr * pArr,int pos,int val)
{
	int i;
	if(is_full(pArr))
	{
		printf("数组已满,插入失败!\n");
		return false;
	}
	if(pos<1||pos>pArr->cnt+1)
		return false;
	for(i=pArr->cnt-1;i>=pos-1;i--)
	{
		pArr->pBase[i+1]=pArr->pBase[i];
	}
	pArr->pBase[pos-1]=val;
	pArr->cnt++;
	return true;
}
bool delete_arr(struct Arr * pArr,int pos,int * pVal)
{
	int i;
	if(is_empty(pArr))
		return false;
	if(pos<1||pos>pArr->cnt)
		return false;
	
	*pVal=pArr->pBase[pos-1];
	for(i=pos;i<pArr->cnt;i++)
	{
		pArr->pBase[i-1]=pArr->pBase[i];
	}
	pArr->cnt--;
	return true;
}
int get_arr(struct Arr * pArr,int pos)
{
	if(pos<1||pos>pArr->cnt)
	{
		printf("您输入的位置有误!\n");
		return 0;
	}
	return pArr->pBase[pos-1];
}
bool replase_arr(struct Arr * pArr,int pos,int val)
{
	if(is_empty(pArr))
	{
		printf("数组为空,没办法替换!");
		return false;
	}
	if(pos<1||pos>pArr->cnt)
	{
		printf("您输入的替换位置有误!");
		return false;
	}
	pArr->pBase[pos-1]=val;
	return true;
} 
void inversion_arr(struct Arr * pArr)
{
	int i=0;
	int j=pArr->cnt-1;
	int t;
	
	while(i<j)
	{
		t=pArr->pBase[i];
		pArr->pBase[i]=pArr->pBase[j];
		pArr->pBase[j]=t;
		i++;
		j--;
	}
	return ;
}
void sort_arr(struct Arr * pArr)
{
	int i,j,t;
	for(i=1;i<pArr->cnt;i++)
	{
		for(j=0;j<pArr->cnt-i;j++)
		{
			if(pArr->pBase[j]>pArr->pBase[j+1])
			{
				t=pArr->pBase[j];
				pArr->pBase[j]=pArr->pBase[j+1];
				pArr->pBase[j+1]=t;
			}
		}
	}
}
bool contains_arr(struct Arr * pArr,int val)
{
	int i,k=0;
	int str[pArr->len]; 
	if(is_empty(pArr))
	{
		printf("数组中无此元素!\n"); 
		return false;
	}
	for(i=0;i<pArr->cnt;i++)
	{
		if(pArr->pBase[i]==val)
		{
			str[k++]=i+1;
		}
	}
	if(k==0)
	{
		printf("数组中无此元素!\n");
		return false;
	}
	else
	{
		printf("数组中有此元素,元素的位置为:");
		for(i=0;i<k;i++)
		{
			printf("%d ",str[i]);
		}
		printf("\n");
		return true;	
	}
}
bool clear_arr(struct Arr * pArr)
{
	if(is_empty(pArr))
	{
		printf("数组中没有值,已经为空!\n");
		return false; 
	}
	pArr->pBase=NULL;
	pArr->cnt=0;
	return true;
}