假设我有下面的列表:
List of CDData
1: Test1
2: Test2
3: Test3
4: Test4
5: Test5
6: Test6
现在我想使用链表从列表中删除第三个:这意味着free(removeFromDList(3((;这是我的功能:
TCD *removeFromDList(int res)
{
int count = 0;
TCD *CDData = First;
CDData->Prev = First;
if (First == NULL)
{
printf("Errorn");
return NULL;
}
while (CDData)
{
count++;
if (count == res)
{
if (count == 1)
{
if (CDData == Last)
Last = NULL;
First = First->Next;
return CDData;
}
else
{
while (CDData != NULL)
{
CDData->Prev = CDData->Next;
if (CDData == Last)
Last = CDData->Prev;
// printf("%s",CDData->Title) I tested here whether my function is going to
// delete the third one or not with the printf() and it's actually printing the third one
// Which means it's correct
return CDData;
}
}
}
else
CDData->Prev = CDData;
CDData = CDData->Next;
}
}
顺便说一句,这就是TCD 的定义
typedef struct F
{
char *Title;
struct F *Next;
struct F *Prev;
}TCD;
现在,在重新打印我的列表后,似乎所有的CDData(整个数据结构(都被释放了。有什么想法吗?
我把它作为输出
List of CDData
我认为在这个结构定义中
typedef struct F
{
char *Titel;
struct F *Next;
struct F *Prev;
}TCD;
您的意思是第一个数据成员的名称是CCD_ 1而不是CCD_。
您需要为该数据成员将指向的字符串动态分配内存。
函数可以调用未定义的行为,至少因为最初全局变量First
(顺便说一句,当函数依赖于全局变量时,这是个坏主意(可以等于NULL
。所以在这个代码片段
TCD *CDData = First;
CDData->Prev = First;
存在使用空指针(CDData->Prev
(来访问存储器的尝试。
这个内部while循环
while (CDData != NULL)
{
CDData->Prev = CDData->Next;
if (CDData == Last)
Last = CDData->Prev;
// printf("%s",CDData->Title) I tested here whether my function is going to
// delete the third one or not with the printf() and it's actually printing the third one
// Which means it's correct
return CDData;
}
没有道理。
请注意,在C中,索引从0开始。
使用您的方法,函数可以按照以下方式编写,如下面的演示程序所示。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct F
{
char *Title;
struct F *Next;
struct F *Prev;
} TCD;
TCD *First, *Last;
TCD * removeFromDList( size_t n )
{
TCD **Current = &First;
while ( *Current && n--)
{
Current = &( *Current )->Next;
}
TCD *target = *Current;
if ( *Current )
{
if ( ( *Current )->Next == NULL )
{
Last = ( *Current )->Prev;
}
else
{
( *Current )->Next->Prev = ( *Current )->Prev;
}
*Current = ( *Current )->Next;
}
return target;
}
int append( const char *s )
{
TCD *Current = malloc( sizeof( TCD ) );
int success = Current != NULL;
if ( success )
{
Current->Title = malloc( strlen( s ) + 1 );
success = Current->Title != NULL;
if ( success )
{
strcpy( Current->Title, s );
Current->Prev = Last;
Current->Next = NULL;
}
else
{
free( Current );
}
}
if ( success )
{
if ( Last )
{
Last = Last->Next = Current;
}
else
{
First = Last = Current;
}
}
return success;
}
FILE * display( FILE *fp )
{
for ( TCD *Current = First; Current; Current = Current->Next )
{
fprintf( fp, "%s -> ", Current->Title );
}
fputs( "NULL", fp );
return fp;
}
int main(void)
{
for ( char s[] = "A"; s[0] != 'E'; ++*s )
{
append( s );
}
fputc( 'n', display( stdout ) );
TCD *p = removeFromDList( 3 );
free( p->Title );
free( p );
fputc( 'n', display( stdout ) );
p = removeFromDList( 1 );
free( p->Title );
free( p );
fputc( 'n', display( stdout ) );
p = removeFromDList( 0 );
free( p->Title );
free( p );
fputc( 'n', display( stdout ) );
p = removeFromDList( 0 );
free( p->Title );
free( p );
fputc( 'n', display( stdout ) );
printf( "First == NULL : %d, Last == NULL : %dn", First == NULL, Last == NULL );
return 0;
}
程序输出为
A -> B -> C -> D -> NULL
A -> B -> C -> NULL
A -> C -> NULL
C -> NULL
NULL
First == NULL : 1, Last == NULL : 1