C 在我的单链表实现中,为什么即使我为要释放的节点分配了内存,指向该节点的指针仍然是';t空?
使用delete_SLL函数,我想删除这个单链接列表的标题(head=4)。虽然我得到了正确的输出,但是保存head值的var struct节点*“temp”不是NULL。自由函数不喜欢的变量“temp”是什么?当将节点温度设置为列表头时,是否未将其Malloc-ed 代码:C 在我的单链表实现中,为什么即使我为要释放的节点分配了内存,指向该节点的指针仍然是';t空?,c,data-structures,C,Data Structures,使用delete_SLL函数,我想删除这个单链接列表的标题(head=4)。虽然我得到了正确的输出,但是保存head值的var struct节点*“temp”不是NULL。自由函数不喜欢的变量“temp”是什么?当将节点温度设置为列表头时,是否未将其Malloc-ed 代码: #include <stdio.h> #include <stdlib.h> struct Node{ int item; struct Node* next; }; struct Li
#include <stdio.h>
#include <stdlib.h>
struct Node{
int item;
struct Node* next;
};
struct List{
struct Node* head;
struct Node* tail;
};
int SLL_empty(struct List* lst){
return lst->head == NULL ;
}
//newLst work
struct List newLst(){
struct List lst;
lst.head = NULL;
lst.tail = NULL;
return lst;
}
//Inserts a node to the front of the list[WORKS]
void insert_SLL(struct List* lst, int x){
struct Node* nde = (struct Node*)malloc(sizeof(struct Node));
nde->next = lst->head;
nde->item = x;
if (SLL_empty(lst))
lst->tail=nde;
lst->head = nde;
}
//Deletes a given Node
void delete_SLL(struct List* lst, int x){
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));;
temp = lst->head;
struct Node* prev = NULL;`enter code here`
//If the head has the key
if (temp != NULL && temp->item == x){
lst->head = temp->next;
temp->next = NULL;
free(temp);
}
// stops once the key is found
while(temp != NULL && temp->item != x){
prev = temp;
temp= temp->next;
}
//If not in list
if (temp == NULL) return;
//If middle
if (temp != NULL && temp->item == x){
prev->next = temp->next;
temp->next = NULL;
}
//if at the end
if (temp != NULL && temp->item == lst->tail->item){
lst->tail= prev;
prev->next = NULL;
}
free(temp);
}
int SLL_pop(struct List *list){
struct Node* nde = list->head;
int item = nde->item;
list->head = nde->next;
free(nde);
if (SLL_empty(list))
list->tail = NULL;
return item;
}
int main(int argc, const char * argv[]) {
int i;
struct List list = newLst();
for (i = 0; i < 5; ++i)
insert_SLL(&list, i);
// printf("The length of the linkedLst is: %d\n",SLL_length(&list));
delete_SLL(&list, 4);
while ( list.head != NULL )
printf("Node: %d\n", SLL_pop(&list));
return 0;
}
#包括
#包括
结构节点{
国际项目;
结构节点*下一步;
};
结构列表{
结构节点*头部;
结构节点*尾部;
};
int SLL_为空(结构列表*lst){
返回lst->head==NULL;
}
//新工作
结构列表newLst(){
结构列表lst;
lst.head=NULL;
lst.tail=NULL;
返回lst;
}
//在列表的前面插入一个节点[WORKS]
void insert_SLL(结构列表*lst,int x){
结构节点*nde=(结构节点*)malloc(sizeof(结构节点));
nde->next=lst->head;
无损检测->项目=x;
如果(SLL_为空(lst))
lst->tail=nde;
lst->head=nde;
}
//删除给定节点
void delete_SLL(结构列表*lst,int x){
结构节点*temp=(结构节点*)malloc(sizeof(结构节点));;
温度=lst->水头;
struct Node*prev=NULL;`在此处输入代码`
//如果头部有钥匙
如果(temp!=NULL&&temp->item==x){
lst->head=temp->next;
temp->next=NULL;
免费(临时);
}
//找到钥匙后停止
while(temp!=NULL&&temp->item!=x){
prev=温度;
温度=温度->下一步;
}
//如果不在列表中
如果(temp==NULL)返回;
//如果中间
如果(temp!=NULL&&temp->item==x){
上一个->下一个=临时->下一个;
temp->next=NULL;
}
//如果最后
if(temp!=NULL&&temp->item==lst->tail->item){
lst->tail=prev;
prev->next=NULL;
}
免费(临时);
}
int SLL_pop(结构列表*列表){
结构节点*nde=list->head;
int item=nde->item;
列表->头部=无损检测->下一步;
免费(nde);
如果(SLL_为空(列表))
list->tail=NULL;
退货项目;
}
int main(int argc,const char*argv[]{
int i;
struct List=newLst();
对于(i=0;i<5;++i)
插入_SLL(&list,i);
//printf(“链接列表的长度为:%d\n”,SLL_长度(&list));
删除列表(&list,4);
while(list.head!=NULL)
printf(“节点:%d\n”,SLL_pop(&list));
返回0;
}
free()的主要用途是要求操作系统将分配的内存带回系统。您可能无法“看到”这一点,但如果您尝试在“temp”之后访问任何元素,则会出现错误。而程序中的“temp”只是一个变量。C不需要,并且由于传递值的意义,无法将给定指针更改为NULL。程序员的工作就是记住这个指针不再有效。
或者,您可以在每次释放指针时手动将其设置为NULL。
struct Node*temp=(struct Node*)malloc(sizeof(struct Node));;温度=lst->水头代码>严重泄漏内存。RedRocketstruct Node*temp=(struct Node*)malloc(sizeof(struct Node))代码>使用malloc()
中的返回值初始化temp
。下一行temp=lst->head代码>然后将temp
分配给一个新值。这两行代码中至少有一行出错。你来这里的目的是什么?。。。但是,如果您尝试在之后访问“temp”中的任何元素,您应该会得到一个错误。如果能这么容易。。。实际上,访问指向已释放内存的指针是未定义的行为。未定义的行为可能就是一切。得到一个错误或崩溃是最幸运的结果(因为,至少,你可以意识到你做错了什么)。。。实际上,这取决于所使用的操作系统,或者您没有监控内存访问的设备。谢谢你的解释。