C语言实现线性存储之连续存储
在数据结构中,数据的存储方式最简单的是线性存储,而线性存储中分为连续存储和链式存储,今天我们实现连续存储
在本程序中使用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,如果那位高人发现了,请及时给与评论指出啊!!谢谢啦!!!
>更多相关文章
- 09-29如何通过wrap malloc定位C/C++程序的内存泄漏
- 02-25打车软件大战升级,补贴还能维持多久?
- 12-23BMP文件右旋90度[c语言]
- 12-23寻找直方图中面积最大的矩形(C语言版)
- 12-23[ndk,2]ndk开发案例和错误处理
- 12-23[ndk,1]ndk开发,C语言入门讲解
- 12-23C语言连续存储实现队列机制
- 12-23Objective-c 数据类型
首页推荐
佛山市东联科技有限公司一直秉承“一切以用户价值为依归
- 01-11全球最受赞誉公司揭晓:苹果连续九年第一
- 12-09罗伯特·莫里斯:让黑客真正变黑
- 12-09谁闯入了中国网络?揭秘美国绝密黑客小组TA
- 12-09警示:iOS6 惊现“闪退”BUG
- 12-05亚马逊推出新一代基础模型 任意模态生成大模
- 12-05OpenAI拓展欧洲业务 将在苏黎世设立办公室
- 12-05微软质疑美国联邦贸易委员会泄露信息 督促其
- 12-05联交所取消宝宝树上市地位 宝宝树:不会对公
- 12-04企业微信致歉:文档打开异常已完成修复
相关文章
24小时热门资讯
24小时回复排行
热门推荐
最新资讯
操作系统
黑客防御