最近了解到了链表的几个基础操作。如下:
创建
LinkList creatList()
{
LinkList r, p, list = NULL;
char data[ 100 ];
FILE *f = fopen( "data.txt", "rb" );
while( fgets( data, 100, f ) )
{
p = ( LinkList )malloc( sizeof( LNode ) );
if( p != NULL ){
strcpy( p->data, data );
p->link = NULL;
if( list == NULL )
list = p;
else
r->link = p;
r = p;
}
}
fclose( f );
return list;
}
链表长度的计算
int length( LinkList list ){
LinkList p = list;
int n = 0;
while( p != NULL ){
n++;
p = p->link;
}
return n;
}
删除一个链表
void deleteList( LinkList *list ){
LinkList p;
p = *list;
while( p != NULL ){
*list = p->link;
free( p );
p = *list;
}
}