C语言实现线性存储之连续存储

浏览:
字体:
发布时间:2013-12-20 16:18:50
来源:

在数据结构中,数据的存储方式最简单的是线性存储,而线性存储中分为连续存储和链式存储,今天我们实现连续存储

在本程序中使用malloc函数构造一个连续存储的内存空间,接着实现对其增,删,改,查,追加,插入等功能,并实现了菜单式界面,由用户自由选择如何对申请到的空间操作

在菜单界面下,用户可以输入q来退出程序

源代码:

/*************************************************************************	> File Name: line_bak.c	> Author: Baniel Gao 	> Mail: createchance@163.com 	> Blog: blog.csdn.net/createchance 	> Created Time: Mon 16 Dec 2013 20:04:58 CST ************************************************************************/#include #include #include void menu(void);int *create_array(void);void init_array(int *array, int n);int check_lenth(int const *array);void add(int *array);void insert(int *array);void modify(int *array);void delete(int *array);void quary(int const *array);void show_array(int const *array);int size;int main(void){	int *array = NULL;	char choice = '/0';	int flag;	array = create_array();	if(array == NULL)	{		return -1;	}	init_array(array,size);	while( choice != 'q' )	{		flag = 0;		system("clear");		printf("Here are some choice: /n");		menu();		printf("Make your choice(press q to quit): ");		while( scanf("%c",&choice ) )		{			if( (choice < '1' || choice > '5' || flag == 1 ) && choice != 'q' )			{				if( choice != '/n' && getchar() != '/n' )					flag = 1;				printf("Make your choice(press q to quit): ");				continue;			}			else				break;			while(getchar() != '/n')				;		}		while(flag && getchar() != '/n')			;		if(choice == 'q')			break;		switch(choice)		{			case '1':				add(array);				show_array(array);				break;			case '2':				show_array(array);				delete(array);				break;			case '3':				show_array(array);				modify(array);				break;			case '4':				show_array(array);				insert(array);				break;			case '5':				quary(array);				break;		}		printf("Press any key to continue...../n");		getchar();	}	free(array);	return 0;}void menu(void){	printf("1).add a number to the array./n");	printf("2).delete a number from the array./n");	printf("3).modify a number of the array./n");	printf("4).insert a number into the array./n");	printf("5).quary a number from the array./n");}int *create_array(void){	int *array = NULL;	printf("Please input the size of the array: ");	while(scanf("%d",&size) != 1)	{		while(getchar() != '/n')			;		printf("The size must be a intger number!Please input again: /n");	}	while(getchar() != '/n')		;	if( (array = (int *)malloc(sizeof(int)*size)) == NULL )	{		printf("Memory allocate failed!/n");	}	return array;}void init_array(int *array, int n){	memset(array,0,n);}int check_lenth(int const *array){	int length = 0;	while(array[length++] != 0)		;	return length-1;}void add(int *array){	int last = check_lenth(array);	if(last == size)	{		printf("The array is full!/n");		return;	}	printf("Please input the number you want to add: ");	while( scanf("%d",array+last) != 1 )	{		while(getchar() != '/n')			;		printf("You must input a intger number!Try again: ");	}	while(getchar() != '/n')		;}void insert(int *array){	int pos = 0, num,i;	int length = check_lenth(array);	if(length == size)	{		printf("The array is full!/n");	}	if(length == 0)	{		printf("The length of the array is 0,you can just add number to it!");		add(array);		return;	}	else	{		printf("Which place do you want to insert?: ");		while(scanf("%d",&pos) != 1 || pos < 1 || pos > length)		{			while(getchar() != '/n')				;			printf("Wrong position!Please input again: /n");		}		printf("Please input the number you want to insert: ");		while( scanf("%d",&num) != 1 )		{			while(getchar() != '/n')				;			printf("Invalid input!Please input again: /n");		}		while(getchar() != '/n')			;		for(i=length-1; i>=pos-1; i--)		{			array[i+1] = array[i];		}		array[pos-1] = num;	}}void modify(int *array){	int pos = 0;	int length = check_lenth(array);	printf("Which place do you want to modify?: ");	while(scanf("%d",&pos) != 1 || pos < 1 || pos > length)	{		while(getchar() != '/n')			;		printf("Wrong position!Please input again: /n");	}	printf("Please input the number you want to change: ");	while( scanf("%d",array+pos-1) != 1 )	{		while(getchar() != '/n')			;		printf("Invalid input!Please input again: /n");	}	while(getchar() != '/n')		;}void delete(int *array){	int pos = 0,i;	int length = check_lenth(array);	if(length == 0)	{		printf("The length is 0,you can not delete!/n");		return;	}	printf("Which place do you want to delete?: ");	while(scanf("%d",&pos) != 1 || pos < 1 || pos > length)	{		while(getchar() != '/n')			;		printf("Wrong position!Please input again: /n");	}	while(getchar() != '/n')		;	for(i=pos-1; i length )			{				while(getchar() != '/n')					;				printf("Invalid position!Please input again!/n");			}			while(getchar() != '/n')				;			printf("The positon %d you quary is : %d!/n",pos,array[pos-1]);		}	}}void show_array(int const *array){	int length = check_lenth(array);	int i = 0;		if(length == 0)	{		printf("The array is NULL!/n");		return;	}	else	{		for(i=0; i

最后,由于本人能力有限,其中肯定会有BUG,如果那位高人发现了,请及时给与评论指出啊!!谢谢啦!!!

>更多相关文章
24小时热门资讯
24小时回复排行
资讯 | QQ | 安全 | 编程 | 数据库 | 系统 | 网络 | 考试 | 站长 | 关于东联 | 安全雇佣 | 搞笑视频大全 | 微信学院 | 视频课程 |
关于我们 | 联系我们 | 广告服务 | 免责申明 | 作品发布 | 网站地图 | 官方微博 | 技术培训
Copyright © 2007 - 2024 Vm888.Com. All Rights Reserved
粤公网安备 44060402001498号 粤ICP备19097316号 请遵循相关法律法规
');})();