本文共 1293 字,大约阅读时间需要 4 分钟。
满意答案
nanhrui57q
2014.01.10
采纳率:57% 等级:12
已帮助:8634人
#include
#include
int compear(void *, void *);
int delEle(int *, int, int);
int main(int argc, char *argv[])
{
int len = 10;
int arrs[10] = {4, 1, 5, 2, 7, 3, 9, 0, 6, 8};
int i;
qsort(arrs, 10, sizeof(int), compear);
for(i = 0; i < 10; i++)
printf("%d ", arrs[i]);
printf("\n");
len = delEle(arrs, 2, len);//delete 2
printf("\n");
len = delEle(arrs, 11, len);//delete 11 but doesn't exist
rdel(arrs, 2, len, 0);
return 1;
}
int compear(void *a, void *b)
{
int *ma = (int *)a, *mb = (int *)b;
if(*ma > *mb)
return 1;
if(*ma == *mb)
return 0;
if(*ma < *mb)
return -1;
}
int delEle(int *arr, int ele, int len)
{
int i, j, found = 0, mlen = len;
for(i = 0; i < len; i++){
if(arr[i] == ele){
for(j = i; j < len - 1; j++){
arr[j] = arr[j + 1];
}
found = 1;
mlen--;
}
}
if(found == 0)
printf("element %d doesn't exists!\n", ele);
for(i = 0; i < mlen; i++)
printf("%d ", arr[i]);
return mlen;//return the length of the arrays
}
//递归删除
int rdel(int *ar, int ele, int len, int flag)//flag为是否前面删除过元素的标记
{
int tmp;
if(len == 1)//数组结束
return *ar;
if(*ar == ele || flag == 1){//发现删除元素或前面删过元素 后面的元素全部前移一位
tmp = *ar;
*ar = rdel(ar + 1, ele, len - 1, 1);
return tmp;
}else{
rdel(ar + 1, ele, len - 1, 0);//前面未删 跳过
}
递归这个比较粗糙 没有边界检查,删最后一个可能有点问题 自己修改下好了
00分享举报
转载地址:http://qzoav.baihongyu.com/