主函数main.c
#include "func.h" #define MAXSIZE 100 INT32 main( void ) { INT32 temp[MAXSIZE] = {NULL}, f = 0, OSM = 1; OSM = create_SL (temp); do { OSM = OSM_Printf("\n\t========================================\n"); OSM = OSM_Printf("\t=============请选择功能选项:============\n"); OSM = OSM_Printf("\t===============1、插入元素===============\n"); OSM = OSM_Printf("\t===============2、删除元素===============\n"); OSM = OSM_Printf("\t===============3、追加元素===============\n"); OSM = OSM_Printf("\t===============4、获取元素===============\n"); OSM = OSM_Printf("\t===============5、查找元素===============\n"); OSM = OSM_Printf("\t===============6、清空元素===============\n"); OSM = OSM_Printf("\t===============7、判断是否空=============\n"); OSM = OSM_Printf("\t===============8、判断是否已满===========\n"); OSM = OSM_Printf("\t===============9、统计元素的个数=========\n"); OSM = OSM_Printf("\t===============0、退出===================\n"); OSM = OSM_Printf("\t**************** 选择数字 ***************\n"); f = scanf_for(); switch(f) { case 1: OSM = Insert_SL (temp); break; case 2: OSM = Delete_SL (temp); break; case 3: OSM = add_SL (temp); break; case 4: OSM = get_SL (temp); break; case 5: OSM = find_SL (temp); break; case 6: OSM = clear_SL (temp); break; case 7: OSM = empty_SL (temp); break; case 8: OSM = full_SL (temp); break; case 9: OSM = number_SL (temp); break; case 0: break; default: OSM = OSM_Printf("输入数据是非法的!!!!\n"); break; } }while (0 != f); return 0; }功能函数:func.c
#include "func.h" #define MAXSIZE 100 /* 开始限定这个顺序表的大小 */ INT32 create_SL(INT32 a[]) /* 创建 */ { INT32 OSM = 1, i32num = 0, i32 = 0; OSM = OSM_Printf("请输入要创建链表中元素的个数:\n"); i32num = scanf_for(); if ( (0 > i32num) || (MAXSIZE < i32num) ) /* 创建的条件 */ { OSM = OSM_Printf(" 非法的数据\n"); } else { OSM = OSM_Printf("输入相关的元素:\n"); for ( i32 = 0; i32 < i32num; i32++) /* 输入的元素 */ { a[i32] = scanf_for(); } } OSM = OSM_Printf("已经创建了表\n"); OSM = OSM_Printf("表为:"); for ( i32 = 0; i32 < i32num; i32++) /* 打印出来 */ { if ( NULL != a[i32] ) { OSM = OSM_Printf(" %d \t",a[i32]); } } i32length = i32; return *a; } INT32 Insert_SL(INT32 b[]) { /* 定义的标识符, 输入的位置, 数组的长度, 插入的数据,循环体变量 */ INT32 OSM = 1, i32Insert_position = 0, i32Insert_data = 0, i32j = 0; OSM = OSM_Printf("输入要插入的位置和数据:\n"); i32Insert_position = scanf_for(); /* 插入的位置 */ if(( 0 > i32Insert_position) || (i32length < i32Insert_position)) /* 插入位置的条件不满足的情况下 */ { OSM = OSM_Printf("非法的数据输入:\n"); } else /* 插入的条件满足的条件下 */ { OSM = OSM_Printf("请输入要插入的数据:\n"); i32Insert_data = scanf_for(); /* 输入插入的数据 */ } i32length++; /* 长度加一 */ for (i32j = i32length; i32j > (i32Insert_position-1); i32j--) /* 位置的后移 */ { b[i32j] = b[i32j - 1]; } b[i32Insert_position-1] = i32Insert_data; for ( i32j = 0; i32j < i32length ;i32j++) /* 打印出来的信息 */ { if (NULL != b[i32j]) { OSM = OSM_Printf("%d\t",b[i32j]); } } OSM = OSM_Printf("\n"); return *b; } INT32 Delete_SL(INT32 a[]) /* 删除 */ { INT32 i32i = 0, OSM = 0, i32delete_position = 0; OSM = OSM_Printf("输入要删除的位置:\n"); i32delete_position = scanf_for(); /* 输入要删除的位置 */ if ((0 > i32length) || (i32delete_position > i32length)) /* 条件 */ { OSM = OSM_Printf("输入的非法数据"); } else { i32length--; /* 长度减一 */ for (i32i = i32delete_position - 1; i32i < i32length; i32i++) /* 进行数据的处理 */ { a[i32i] = a[i32i+1]; } } for ( i32i = 0; i32i < i32length ;i32i++) /* 打印出来的信息 */ { if (0 <= i32i) { if (NULL != a[i32i]) { OSM = OSM_Printf("%d\t",a[i32i]); } } } return *a; } INT32 add_SL(INT32 a[]) /* 追加 */ { INT32 OSM = 1, i32add_data, i32i; OSM = OSM_Printf("输入要追加的数据:\n"); i32add_data = scanf_for(); /* 输入追加的元素 */ i32length++; /* 长度加1 */ a[i32length -1] = i32add_data; /* 直接赋值 */ for ( i32i = 0; i32i < i32length; i32i++) /* 打印出来的信息 */ { if (NULL != a[i32i]) { OSM = OSM_Printf("%d\t",a[i32i]); } } return *a; } INT32 get_SL(INT32 a[]) /* 获取元素 */ { INT32 OSM = 1, i32location = 0; OSM = OSM_Printf("选择需要获位置\n"); i32location = scanf_for(); if (i32location > i32length) { OSM = OSM_Printf("超出范围了\n"); } else { if ( (NULL != (i32location - 1)) && (NULL != a)) { OSM = OSM_Printf("获取的数据为: %d \t",a[i32location - 1]); } } return 0; } INT32 find_SL(INT32 a[]) /* 查找 */ { INT32 OSM = 1,flag = 0, i32i = 0, i32elem = 0; OSM = OSM_Printf("输入需要查找元素:\t"); i32elem = scanf_for(); OSM = OSM_Printf("查询的位置是:"); for (i32i = 0; i32i < i32length; i32i++) { if ( i32elem == a[i32i]) { OSM = OSM_Printf(" %d ",i32i + 1); flag = 1; } } if (1 != flag) { OSM = OSM_Printf("没有找到想要的数据!!!!!!!\n"); } return *a; } INT32 clear_SL(INT32 a[]) /* 清空 */ { a[i32length] = NULL; i32length = 0; return 0; } INT32 empty_SL(INT32 a[]) /* 判读是否为空 */ { INT32 OSM = 1; if ( 0 == i32length) /* 判断第一个是否为空 */ { OSM = OSM_Printf("是空的\n"); } else { OSM = OSM_Printf(" 不是空的 \n"); } return 0; } INT32 full_SL(INT32 a[]) /* 是否已满 */ { INT32 OSM = 1; if ( MAXSIZE > i32length) /* 判断最后一个元素是否为空 */ { OSM = OSM_Printf("没有满\n"); } else { OSM = OSM_Printf("已经满了\n"); } return 0; } INT32 number_SL(INT32 a[]) /* 元素的个数 */ { INT32 OSM = 1; if ( 0 <= i32length ) { OSM = OSM_Printf("元素的个数为%d\n",i32length); } return 0; }
原文地址:http://blog.csdn.net/u011046042/article/details/41948453