|
楼主 |
发表于 2014-8-23 14:03:08
|
显示全部楼层
楼主你的程序问题很多,参考一下我改正过得
#include<stdio.h>
#define MAXNUM 50
typedef int elemType ;
struct staticlist{
elemType data ;
int cur ;
}sl[MAXNUM] ;
typedef struct staticlist *psl ;
// 1静态数组初始化
void initlist( psl L )
{
int i ;
for( i = 0 ; i< MAXNUM - 2 ; i++ )
{
L[i].cur = i+1 ;
}
L[MAXNUM -2 ].cur = 0 ;
L[MAXNUM-1].data = 0 ;
L[MAXNUM-1].cur = 1 ; // 第一个可以利用的备用空间
}
/*
void creatstaticlist( psl L , int num )
{
if( num < 0 || num > MAXNUM - 2 )
{
printf("num value is not right , please check!\n") ;
return ;
}
int i , j = 0 ;
for( i = 0 ; i< num ; i++ )
{
j = L[j].cur ;
printf("input %d pos value \n", i+1 ) ;
scanf("%d",&L[j].data ) ;
}
L[j].cur = 0 ;
printf("output L[%d].cur= %d\n",i, L[j].cur ) ;
L[MAXNUM-1].cur = num + 1 ;
printf("the first space %d ",L[MAXNUM-1].cur) ;
}
*/
void creatstaticlistnum( psl L )
{
int j = 0 , i = 0 ;
int value ;
printf("input value above 0 to creat static list");
while( scanf("%d",&value) && value > 0 )
{
i = L[i].cur ;
L[i].data = value ;
//printf("%d\n",L[j].data ) ;
//printf("L[i].cur= %d\n",L[i].cur) ;
printf("input value above 0 to creat static list");
if(++j > MAXNUM -2 )
break ;
}
L[MAXNUM-1].cur =L[i].cur ;
L[i].cur = 0 ;
printf("first free space is %d",L[MAXNUM-1].cur) ;
}
void printstaticlist( psl L )
{
int i = L[0].cur ;
while( i != 0 )
{
printf("print static list number %d %d\n", i , L[i].data) ;
i = L[i].cur ;
}
}
int lenstaticlist( psl L )
{
int listlen = 0 ;
int i = L[0].cur ;
while( i != 0 )
{
listlen++ ;
i = L[i].cur ;
}
return listlen ;
}
void insertstaticlistpos( psl L , int pos , int value )
{
int count = 0 , j = 0 , i = MAXNUM-1 ;
if( pos < 1 || pos > lenstaticlist( L ) )
{
printf("pos value is not proper , please check ") ;
return ;
}
i = L[MAXNUM-1].cur ;
L[MAXNUM-1].cur = L[i].cur ;
L[i].data = value ;
if( pos == 1 )
j = 0 ;
else
{
while( ( j = L[j].cur) != 0 )
{
if( ++count == pos -1 )
break ;
}
}
L[i].cur = L[j].cur ;
L[j].cur = i ;
}
int main( void )
{
int pos , value ;
int lensl ;
//int num ;
initlist( sl ) ; //没问题
//printf("input the num") ;
//scanf("%d",&num ) ;
creatstaticlistnum( sl ) ; // ok
printstaticlist( sl ) ; // ok
printf("input a pos value and insert value\n") ;
scanf("%d%d",&pos,&value) ;
insertstaticlistpos( sl , pos , value ) ;
lensl = lenstaticlist( sl ) ;
printf("the len of the staticlist is %d\n",lensl) ;
printstaticlist( sl ) ;
return 0 ;
}
|
|