在 C 的 Linkedlist 实现中,head 没有被设置为 NULL
In the implementation of Linkedlist in C, head is not being set NULL
我正在尝试使用 struct linkedlist 和 struct node(如下所示)在 C 中实现链表。
当我调用 new_list()
方法时,我明确地将 list->head
设置为 NULL
。现在,我尝试使用 add_elem()
方法添加第一个元素。后来,我使用 print_list()
函数打印列表的第一个元素。
在 add_elem()
函数中,我检查:如果头部是 NULL
并且索引(添加节点的位置)为零,则创建头部并设置值。但是,当我执行代码时,list->head==NULL
是 FALSE
。
为什么 head 不是 NULL
,即使在明确设置后 NULL
?
#include<stdio.h>
#include<stdlib.h>
struct node{
int value;
struct node *next;
};
struct linkedlist{
int size;
struct node *head;
};
void new_list(struct linkedlist *input);
int get_elem(struct linkedlist *list, int index);
int add_elem(struct linkedlist *list, int index, int value);
void remove_elem(int index);
void print_list(struct linkedlist *list);
int main(){
struct linkedlist *mylist;
printf("starting the program \n");
// Creating an empty list
new_list(mylist);
int index = 0;
int value = 2;
// adding an element (currently only supports of adding a head)
add_elem(mylist, index, value);
// print the head
print_list(mylist);
return 0;
}
void new_list(struct linkedlist* input){
printf("Creating new list\n");
input = (struct linkedlist*) malloc(sizeof(struct linkedlist));
input->size = 0;
//input->head = (struct node *)malloc(sizeof(struct node));
input->head = NULL;
}
int add_elem(struct linkedlist *list, int index, int value){
// If i remove "list->head==NULL" condition, it works
// otherwise it goes into else block, instead of executing if block
if(list->head==NULL && index==0){
printf("Adding first elem\n");
struct node *nodeptr;
nodeptr =(struct node *) malloc(sizeof(struct node));
nodeptr->value=value;
nodeptr->next = NULL;
list->head = nodeptr;
list->size=1;
return 1;
}
else {
// handle later
return -1;
}
}
void print_list(struct linkedlist *list){
if(list!=NULL && list->head!=NULL){
struct node *ptr = list->head;
//while(ptr!=NULL)
{
printf("Head value:%d\n",ptr->value);
ptr= ptr->next;
}
}
}
编辑:
我按照建议将函数 new_list( ) 更改为 return 新分配的地址直接作为 return 值。现在代码可以正常工作了。
struct linkedlist* new_list(void){
printf("Creating new list\n");
return (struct linkedlist*) malloc(sizeof(struct linkedlist));
}
new_list
分配了一个新的头节点,但是将其分配给 input
不会 改变 mylist
的值。因此,add_elem
并没有查看您分配和分配给的节点;它会查看 mylist
恰好指向的任何东西,这可能是任何东西,因为你从未初始化它(我想 new_list
会为你做这件事)。
我正在尝试使用 struct linkedlist 和 struct node(如下所示)在 C 中实现链表。
当我调用 new_list()
方法时,我明确地将 list->head
设置为 NULL
。现在,我尝试使用 add_elem()
方法添加第一个元素。后来,我使用 print_list()
函数打印列表的第一个元素。
在 add_elem()
函数中,我检查:如果头部是 NULL
并且索引(添加节点的位置)为零,则创建头部并设置值。但是,当我执行代码时,list->head==NULL
是 FALSE
。
为什么 head 不是 NULL
,即使在明确设置后 NULL
?
#include<stdio.h>
#include<stdlib.h>
struct node{
int value;
struct node *next;
};
struct linkedlist{
int size;
struct node *head;
};
void new_list(struct linkedlist *input);
int get_elem(struct linkedlist *list, int index);
int add_elem(struct linkedlist *list, int index, int value);
void remove_elem(int index);
void print_list(struct linkedlist *list);
int main(){
struct linkedlist *mylist;
printf("starting the program \n");
// Creating an empty list
new_list(mylist);
int index = 0;
int value = 2;
// adding an element (currently only supports of adding a head)
add_elem(mylist, index, value);
// print the head
print_list(mylist);
return 0;
}
void new_list(struct linkedlist* input){
printf("Creating new list\n");
input = (struct linkedlist*) malloc(sizeof(struct linkedlist));
input->size = 0;
//input->head = (struct node *)malloc(sizeof(struct node));
input->head = NULL;
}
int add_elem(struct linkedlist *list, int index, int value){
// If i remove "list->head==NULL" condition, it works
// otherwise it goes into else block, instead of executing if block
if(list->head==NULL && index==0){
printf("Adding first elem\n");
struct node *nodeptr;
nodeptr =(struct node *) malloc(sizeof(struct node));
nodeptr->value=value;
nodeptr->next = NULL;
list->head = nodeptr;
list->size=1;
return 1;
}
else {
// handle later
return -1;
}
}
void print_list(struct linkedlist *list){
if(list!=NULL && list->head!=NULL){
struct node *ptr = list->head;
//while(ptr!=NULL)
{
printf("Head value:%d\n",ptr->value);
ptr= ptr->next;
}
}
}
编辑:
我按照建议将函数 new_list( ) 更改为 return 新分配的地址直接作为 return 值。现在代码可以正常工作了。
struct linkedlist* new_list(void){
printf("Creating new list\n");
return (struct linkedlist*) malloc(sizeof(struct linkedlist));
}
new_list
分配了一个新的头节点,但是将其分配给 input
不会 改变 mylist
的值。因此,add_elem
并没有查看您分配和分配给的节点;它会查看 mylist
恰好指向的任何东西,这可能是任何东西,因为你从未初始化它(我想 new_list
会为你做这件事)。