您的位置:首页 > 其它

以冒泡排序为例--malloc/free 重定向stdin stdout

2016-04-24 19:48 465 查看

参考

C语言冒泡排序算法及代码 有详细解释

《数据结构》 陈越 等编著

esort.c 代码如下,可关注下mallloc/free,freopen重定向的用法,排序为每轮将最小的数放在前面:

#include<stdio.h>
#include<malloc.h>

#define    N   100

int  datin(int *p);
void printout(int *p,int len);
void esort(int *p,int len);

int main(void)
{
int len=0;
int *a,*ptrinit;
a = (int *)malloc(sizeof(int)*N);
ptrinit = a;
if(a == NULL)
{
printf("a malloc err!\n");
return -1;
}
len = datin(a);
printf("\n input data len = %d:\n",len);
printf("\n input data :\n");
printout(a,len);
esort(a,len);
printf("\n esort data :\n");
printout(a,len);

free(a);
printf("\n--exit--\n");
return 1;
}

void esort(int *p,int len)
{
int i=0,j=0;
int temp = 0;
for(i=0; i<len; i++)
for(j=0; j<len-i-1; j++)
{
if(p[j] > p[j+1])
{
temp = p[j+1];
p[j+1] = p[j];
p[j] = temp;
}
}
}

int datin(int *p)
{
int len =0 ;
freopen("data.txt","r",stdin);  //data input
while(scanf("%d",p)!=EOF)
{
len++;
p++;
}
fclose(stdin);
return len;
}

void printout(int *p,int len)
{
int i=0;
while(i<len)
{
printf("%d\t",*p);
p++;
i++;
}
}


注意如下函数,形参int *p为整型指针类型:

int datin(int *p)
{
int len =0 ;
freopen("data.txt","r",stdin);  //data input
while(scanf("%d",p)!=EOF)
{
len++;
p++;
}
fclose(stdin);
return len;
}


1)在输入和输出数据的函数中,指针做形参时,在函数中是不会修改指针的地址的,只能修改指针所指向的内容,即 *p。

2)结合栈(stack)的理解,函数传递的参数和临时变量都是保存在 栈里面的,在函数退出时,栈里面的内容会被释放掉。

3)所以,指针的地址是不会被改变的,就算是 在函数中做了p++的操作。通过scanf改变了 (*p)的数值

Makefile

CC := gcc

all:esort_min esort_max

esort_min: esort_min.o
$(CC) $^ -o $@

esort_min.o: esort_min.c
$(CC) -c $^

esort_max: esort_max.o
$(CC) $^ -o $@

esort_max.o: esort_max.c
$(CC) -c $^

clean:
rm -f *.o  esort_min esort_max


执行情况:

hy@ubuntu:~/ccc/pjesort$ ./esort_min

input data len = 17:

input data :
255    168    90    1    230    90    171    10    32    32    190    982    3984    4395    39082    0    32
esort data :
0    1    10    32    32    32    90    90    168    171    190    230    255    982    3984    4395    39082
--exit--
hy@ubuntu:~/ccc/pjesort$ ./esort_max

input data len = 17:

input data :
255    168    90    1    230    90    171    10    32    32    190    982    3984    4395    39082    0    32
esort data :
39082    4395    3984    982    255    230    190    171    168    90    90    32    32    32    10    1    0
--exit--
hy@ubuntu:~/ccc/pjesort$


冒泡排序改进算法

// coding by microhy
//
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
//#include <ctype.h>
#define   MAXLEN     (100)

int data_input(int *a)
{
int *ptri=a;
int cnt = 0;
if(ptri==NULL)
{
return -1;
}
while(scanf("%d",ptri)!=EOF)
{
ptri++;
cnt++;
}
return cnt;
}

void data_output(int *a,int N)
{
int i=0;
while(i<N)
{
printf("%d\t",*a);
i++;
a++;
}
printf("\n");
}

void BuddleSort(int *a,int N)
{
int i,j,temp;
int flag=0;
for(i=0; i<N; i++)
{
flag=0;
for(j=0; j<N-i-1; j++)
{
if(a[j] > a[j+1])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
flag = 1;
}
}
if(!flag)
break;
}
}

int main()
{
int dat_len=0;
int *datArry=NULL;

datArry = (int *)malloc(sizeof(int)*MAXLEN);
if(datArry==NULL)
{
printf("[err]NULL, Pls Check datArry malloc\n");
return -1;
}
#if 1    // !!
freopen("in.txt","r",stdin);
printf("\t!!! < Pls freopen off > !!!\n");
//freopen("out.txt","w",stdout);
#endif //

dat_len = data_input(datArry);
if(dat_len&&dat_len<=MAXLEN)
{
data_output(datArry,dat_len);
BuddleSort(datArry,dat_len);
data_output(datArry,dat_len);
}
free(datArry); // !!!
return 0;
}


代码链接:

http://pan.baidu.com/s/1pLM0ILH

http://pan.baidu.com/s/1slHkUAD
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: