You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <stdio.h>
# include <stdlib.h>
# define list_init_size 100
# define listincrement 10
# define ok 1
typedef int elemtype ;
typedef int status ;
typedef struct
{
elemtype * elem ;
int length ;
int listsize ;
} sqlist ;
status initlist ( sqlist & l ) //构造空线性表
{
l . elem = ( elemtype * ) malloc ( list_init_size * sizeof ( elemtype ) ) ;
l . listsize = list_init_size ;
l . length = 0 ;
return ok ;
}
status listlength ( sqlist l ) //得到元素的个数
{
return l . length ;
}
int getelem ( sqlist l , int i , elemtype & e ) //得到数值
{
if ( i < 1 | | i > l . length )
{
printf ( " 不合法 " ) ;
return 0 ;
}
e = l . elem [ i - 1 ] ;
return ok ;
}
status listinsert ( sqlist & l , int i , elemtype e ) //插入
{
elemtype * p , * q ;
q = & ( l . elem [ i - 1 ] ) ;
for ( p = & ( l . elem [ l . length - 1 ] ) ; p > = q ; - - p )
* ( p + 1 ) = * p ;
* q = e ;
+ + l . length ;
return ok ;
}
void listprint ( sqlist l ) //输出
{
int i ;
for ( i = 0 ; i < l . length ; i + + )
printf ( " %d " , l . elem [ i ] ) ;
printf ( " \n " ) ;
}
void mergelist ( sqlist la , sqlist lb , sqlist & lc )
{
int i , j , k , ai , bj ;
initlist ( lc ) ;
i = j = 1 ;
k = 0 ;
la . length = listlength ( la ) ;
lb . length = listlength ( lb ) ;
while ( ( i < = la . length ) & & ( j < = lb . length ) )
{
getelem ( la , i , ai ) ;
getelem ( lb , j , bj ) ;
if ( ai < = bj )
{
listinsert ( lc , + + k , ai ) ;
+ + i ;
}
else
{
listinsert ( lc , + + k , bj ) ;
+ + j ;
}
}
while ( i < = la . length )
{
getelem ( la , i + + , ai ) ;
listinsert ( lc , + + k , ai ) ;
}
while ( j < = lb . length )
{
getelem ( lb , j + + , bj ) ;
listinsert ( lc , + + k , bj ) ;
}
}
int main ( )
{
sqlist La , Lb , Lc ;
int i , j ;
elemtype x ;
initlist ( La ) ;
printf ( " 请输入La的长度: " ) ;
scanf ( " %d " , & i ) ;
printf ( " 请输入La中的元素: " ) ;
for ( j = 1 ; j < = i ; j + + )
{
scanf ( " %d " , & x ) ;
listinsert ( La , j , x ) ;
}
printf ( " 输出La中的元素: " ) ;
listprint ( La ) ;
initlist ( Lb ) ;
printf ( " \n 请输入Lb的长度: " ) ;
scanf ( " %d " , & j ) ;
printf ( " 请输入Lb中的元素: " ) ;
for ( i = 1 ; i < = j ; i + + )
{
scanf ( " %d " , & x ) ;
listinsert ( Lb , i , x ) ;
}
printf ( " 输出Lb中的元素: " ) ;
listprint ( Lb ) ;
mergelist ( La , Lb , Lc ) ;
printf ( " \n 输出合并后的顺序表Lc: " ) ;
listprint ( Lc ) ;
return 0 ;
}