呈现的函数存在逻辑错误。
当位置设置为等于1 时,由于此代码sn-p,函数会在当前head 节点之前插入一个新节点
if(n<=1){
new->next = head;
return new;
}
因此,例如,如果您有一个空列表(head 等于 NULL),则调用类似的函数
head = insertatspecificposition( a_new_node_with_value_1, 1 );
你会得到一个看起来像的列表
1 -> null
现在逻辑上是一致的,如果在下一次调用函数时指定位置2,您将获得如下所示的列表
1 -> 2 -> null
不过这样的电话
head = insertatspecificposition( one_more_new_node_with_value_2, 2 );
返回NULL,而不是返回指针head的当前值。
这是一个演示程序。
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *head;
struct node * create( int data )
{
struct node *new = malloc( sizeof( struct node ) );
if ( new != NULL )
{
new->data = data;
new->next = NULL;
}
return new;
}
struct node *insertatspecificposition( struct node *new,int n){
struct node *pred = head;
if(n<=1){
new->next = head;
return new;
}
while(--n && pred != NULL ){
pred = pred->next;
}
if(pred == NULL){
return NULL;
}
new->next = pred->next;
pred->next = new;
return head;
}
void display( void )
{
for ( struct node *current = head; current != NULL; current = current->next )
{
printf( "%d -> ", current->data );
}
puts( "null" );
}
int main(void)
{
display();
head = insertatspecificposition( create( 1 ), 1 );
display();
head = insertatspecificposition( create( 2 ), 2 );
display();
return 0;
}
程序输出是
null
1 -> null
null
而不是预期的输出
null
1 -> null
1 -> 2 -> null
还要注意该函数应该检查传递的指针new是否不等于NULL。
当函数依赖于全局变量时,这不是一个好的设计。
就我而言,我会按照下面的演示程序所示的方式编写函数。
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node * create( int data )
{
struct node *new = malloc( sizeof( struct node ) );
if ( new != NULL )
{
new->data = data;
new->next = NULL;
}
return new;
}
int insertatspecificposition( struct node **head, int data, size_t n )
{
struct node *new_node = create( data );
int success = new_node != NULL;
if ( success )
{
while ( n-- && *head != NULL )
{
head = &( *head )->next;
}
new_node->next = *head;
*head = new_node;
}
return success;
}
FILE * display( const struct node *head, FILE *fp )
{
for ( ; head != NULL; head = head->next )
{
fprintf( fp, "%d -> ", head->data );
}
fputs( "null", fp );
return fp;
}
int main(void)
{
struct node *head = NULL;
putc( '\n', display( head, stdout ) );
insertatspecificposition( &head, 1, 0 );
putc( '\n', display( head, stdout ) );
insertatspecificposition( &head, 3, 1 );
putc( '\n', display( head, stdout ) );
insertatspecificposition( &head, 2, 1 );
putc( '\n', display( head, stdout ) );
insertatspecificposition( &head, 4, 3 );
putc( '\n', display( head, stdout ) );
return 0;
}
程序输出是
null
1 -> null
1 -> 3 -> null
1 -> 2 -> 3 -> null
1 -> 2 -> 3 -> 4 -> null
如果指定位置大于或等于列表中的节点数,则将新节点附加到列表的尾部。
在 C 中作为数组索引的位置从 0 开始。
当然,您至少还需要一个函数来清除列表,该列表将释放所有已分配的内存。