顺序线性表

来源:互联网 发布:400外呼软件 编辑:程序博客网 时间:2024/06/07 15:14
#include <stdio.h>int const MAXSIZE = 100;typedef int element;typedef struct { element *es; int size; int max; } list;element list_get_index_value(list * , int );int main(){ //函数声明区 void list_init( list * , int ); void list_destory( list * ); void list_end_insert( list * , element ); int list_length(list *); void list_head_insert( list * , element ); void list_index_insert( list * , element , int ); void list_index_update( list * , element , int ); void list_index_delete( list * , int ); int list_return_value_index(list * , element ); void list_print(list *); void list_mp_sort(list *); void list_combine_sect(list * , list * ); void list_add_another_l(list * , list * ); //测试输入 list ls; list_init( &ls , MAXSIZE ); list_end_insert(&ls , 9 ); list_end_insert(&ls , 7 ); list_end_insert(&ls , 4 ); list_end_insert(&ls , 13 ); list_end_insert(&ls , 22 ); list_end_insert(&ls , 4 ); list_end_insert(&ls , 6 ); list_head_insert(&ls, 5 ); list_head_insert(&ls, 14 ); list_head_insert(&ls, 19 ); list_head_insert(&ls, 5 ); list_index_insert(&ls, 7 , 1); list_index_update(&ls, 2 , 1); list_index_delete(&ls, 4); list_mp_sort(&ls);  //显示  list_print(&ls); printf("7第一次出现的位置为%d\n" , list_return_value_index(&ls , 7) );  //list 求并集  list ls_1; list_init( &ls_1 , MAXSIZE ); list_end_insert(&ls_1 , 99 ); list_end_insert(&ls_1 , 98 ); list_end_insert(&ls_1 , 97 ); list_end_insert(&ls_1 , 96 ); list_end_insert(&ls_1 , 14 ); list_mp_sort(&ls_1); list_print(&ls_1); list_combine_sect(&ls , &ls_1 ); list_print(&ls);  //中断  list_destory(&ls); getchar(); return 0;}//************************************函数**************************************//初始化线性表void list_init( list *ls , int maxsize ){ if(maxsize <= 0){printf("初始化出错.....\n");return;} ls->es = ( element * )malloc( maxsize * sizeof(element) ); ls->size = 0; ls->max = maxsize;}//线性表销毁void list_destory( list *ls ){ free( ls->es ); ls->size = ls->max = 0;}//从表尾插入数据void list_end_insert( list *ls , element e ){ if(ls->size >= ls->max){printf("已到表的最大值,不能再插入新数据了....\n");return;} *((ls->es)+(ls->size)) = e ; ls->size = ls->size + 1;}//从表头插入数据void list_head_insert( list *ls , element e ){ if(ls->size >= ls->max){printf("已到表的最大值,不能再插入新数据了....\n");return;} int i = ls->size; while( i > 0 ) {  *((ls->es)+(i)) = *((ls->es)+i-1);  --i; } *(ls->es) = e; ls->size = ls->size + 1;}//从第index个位置插入数据,index的位置为0到MAXSIZE-1 void list_index_insert( list *ls , element e , int index ){ if(ls->size >= ls->max){printf("已到表的最大值,不能再插入新数据了....\n");return;} if( index > (ls->size-1) || index < 0 ){printf("插入的位置不对....\n");return;} int i = ls->size; while( i > index ) {  *((ls->es)+i) = *((ls->es)+i-1);  --i; } *(ls->es+index) = e; ls->size = ls->size + 1;}//修改线性表上的第index个值,index值必须是有意义的数 void list_index_update( list *ls , element e , int index ){ if( index > (ls->size-1) || index < 0 ){ printf("该位置上没有值....\n");return; } *((ls->es)+index) = e;}//删除线性表上第index个值,其后的元素自动向前一位,size自动减一 ,index值必须是有意义的数 void list_index_delete( list *ls , int index ){ if( index > (ls->size-1) || index < 0 ){ printf("那个位置上不存在元素....\n");return; } while( index <= (ls->size - 1)) {  *((ls->es)+index) = *((ls->es)+index+1);  index++; } ls->size = ls->size - 1;}//求得此线性表的长度int list_length(list *ls){ return ls->size;}//获取线性表上第index上的值 element list_get_index_value(list *ls , int index){ if( index > (ls->size-1) || index < 0 ) { printf("那个位置上不存在元素....\n");return; } return *(ls->es + index);}//返回与指定值相匹配的第一次出现的元素的位置--顺序查找int list_return_value_index(list *ls , element e ){ int i = 0; while( i < ls->size ) {  if( *(ls->es + i) == e ) return i;  i++; } return -1;}//打印所有元素 void list_print(list *ls){ int i;    for( i = 0 ; i < ls->size ; i++ ) printf("[%d]" , *(ls->es + i) ); printf("\n");}//冒泡法排序void list_mp_sort(list *ls){ int i; int j; for(i = ls->size-1 ; i >= 0 ; i--) {  for(j = 0 ; j < i ; j++)  {   if( list_get_index_value(ls, j) > list_get_index_value(ls, j+1) )   {    int tmp;    tmp = list_get_index_value(ls,j+1);    list_index_update(ls , list_get_index_value(ls,j) , j+1 );    list_index_update(ls , tmp , j );   }  } }}//后面的线性表加的前面的线性表的尾部 void list_add_another_l(list *ls , list *_ls ){ int i; for(i=0 ; i < _ls->size ; i++) {   list_end_insert(ls , list_get_index_value(_ls , i)); }}//两线性表的并集记过放在一个ls里 void list_combine_sect(list *ls , list *_ls ){ int i; for(i=0 ; i < _ls->size ; i++) {  if( list_return_value_index(ls ,list_get_index_value(_ls , i)) == -1 )  {   list_end_insert(ls , list_get_index_value(_ls , i));  } }}

定义顺序线性表,基本函数,和基本操作


原创粉丝点击