【发布时间】:2019-09-11 03:12:19
【问题描述】:
在尝试删除节点时,我能够释放分配的内存,但无法将其分配给 NULL,因此我的程序停止打印这些地址。我被引导相信我必须使用双指针来做到这一点。
我尝试在释放后等于 NULL,但这会使程序崩溃。
typedef struct Linked_List_type
{
struct Node_type *first;
struct Node_type *last;
} LinkedList;
typedef struct Node_type
{
int data;
char name[15];
char phone[15];
struct Node_type *next;
struct Node_type *prev;
} Node;
//This is how I insert
void LinkedList_insert (LinkedList * this, int val, char * name, char * phone)
{
//creo un nodo para insertar
Node *it = this->first;
Node *newNode = new_Node (val, name, phone);
if (!this)
{
printf ("Lista no creada\n");
}
for (it = this->first; it != NULL; it = it->next)
{
if (strcmp(it->name, name)==0) //Evitar repeticiC3n
{
printf ("Valor repetido\n");
return;
}
}
if (!this->first)
{
newNode->next = NULL;
this->last = newNode;
this->first = newNode;
//first y last ocupan el mismo valor, siguiente apunta a nulo
//solo si la lista estC! vacC-a
}
else
{
newNode->prev = this->last;
this->last->next = newNode;
this->last = this->last->next;
}
}
//This is how I remove
Bool LinkedList_removestr(LinkedList * this, char * str)
{
Node * tmp = NULL;
Node * it = this->first;
Bool Bandera = FALSE;
while(it!=NULL)
{
if(it->next!=NULL && strcmp(it->name,str)==0)
{
tmp=it;
it=it->next;
free(tmp);
Bandera=TRUE;
}
if(it->next==NULL && strcmp(it->name,str)==0)//si first es igual al que quiero remover
{
tmp=it;
free(tmp);//no apunto a next porque no hay
Bandera=TRUE;
}
it=it->next;
}
return Bandera;
}
删除后,打印列表时,我希望列表跳过被删除的节点地址,或者不显示,但是使用我的删除功能后,打印列表时,节点打印地址,我相信。
【问题讨论】:
标签: c