[英]In the implementation of Linkedlist in C, head is not being set NULL
I am trying to implement linkedlist in C, using struct linkedlist and struct node (as implemented below).我正在尝试使用struct linkslist和struct node (如下实现)在 C 中实现链表。
While i call new_list()
method, i explicitly set list->head
as NULL
.当我调用
new_list()
方法时,我明确地将list->head
设置为NULL
。 Now, I try to add the first element using add_elem()
method.现在,我尝试使用
add_elem()
方法添加第一个元素。 And later, I print the first element of the list using print_list()
function.稍后,我使用
print_list()
函数打印列表的第一个元素。
In the add_elem()
function I check: if the head is NULL
and index (where the node is being added) is zero, then create the head and set the value.在
add_elem()
函数中,我检查:如果头部为NULL
并且索引(添加节点的位置)为零,则创建头部并设置值。 But, while I execute the code, list->head==NULL
is FALSE
.但是,当我执行代码时,
list->head==NULL
是FALSE
。
Why head is not NULL
, despite even after setting it NULL
explicitly?为什么 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;
}
}
}
EDIT:编辑:
I changed the function new_list( ) as suggested to return the newly allocated address directly as a return value.我按照建议更改了函数 new_list() 以将新分配的地址直接作为返回值返回。 And now the code works correctly.
现在代码可以正常工作了。
struct linkedlist* new_list(void){
printf("Creating new list\n");
return (struct linkedlist*) malloc(sizeof(struct linkedlist));
}
new_list
allocates a new head node, but assigning it to input
does not change the value of mylist
. new_list
分配一个新的头节点,但它分配给input
不改变的值mylist
。 Thus, add_elem
isn't looking at the node you allocated and assigned to;因此,
add_elem
不会查看您分配和分配给的节点; its looking at whatever mylist
happens to point to, which could be anything, since you never initialized it (expecting new_list
to do it for you, I suppose).它查看
mylist
碰巧指向的任何内容,这可能是任何内容,因为您从未初始化过它(我想,希望new_list
为您做这件事)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.