设为首页 加入收藏

TOP

C语言实现线性存储之连续存储(一)
2014-11-23 21:27:34 来源: 作者: 【 】 浏览:14
Tags:语言 实现 线性 存储 连续

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

在本程序中使用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
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇纯C语言写的日历 下一篇山东理工大学ACM平台题答案关于C..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: