My approach:
我的方法:
An array of fixed-length (lets say 20) each element is pointer to the first node of a linked list. so i have 20 different linked list.
每个元素的固定长度(假设为20)数组是指向链表的第一个节点的指针。所以我有20个不同的链表。
This is the structure:
这是结构:
struct node{
char data[16];
struct node *next;
};
My declaration for that array
我对该数组的声明
struct node *nodesArr[20];
now to add a new node to one of the linked list, i do this:
现在要将一个新节点添加到其中一个链表,我这样做:
struct node *temp;
temp = nodesArr[i]; // i is declared and its less than 20
addNode(temp,word); // word is declared (char *word) and has a value ("hello")
The addNode function:
addNode函数:
void addNode(struct node *q, char *d){
if(q == NULL)
q = malloc(sizeof(struct node));
else{
while(q->next != NULL)
q = q->next;
q->next = malloc(sizeof(struct node));
q = q->next;
}
q->data = d; // this must done using strncpy
q->next = NULL;
}
and to print data from the array of linked list, i do this:
并从链表列表中打印数据,我这样做:
void print(){
int i;
struct node *temp;
for(i=0 ; i < 20; i++){
temp = nodesArr[i];
while(temp != NULL){
printf("%s\n",temp->data);
temp = temp->next;
}
}
}
now compiler gives no error, the program run and i pass the data to it, and when i call print it doesn't print any thing,,??
现在编译器没有错误,程序运行,我将数据传递给它,当我打电话打印它不打印任何东西,??
UPDATE::
更新::
after I edited the code (thx for you), i think the problem in the print function,, any idea ?
在我编辑代码(thx for you)之后,我认为打印功能中的问题,任何想法?
4 个解决方案
#1
5
The problem lies in addNode()
. When the list is empty you do:
问题在于addNode()。当列表为空时,您执行以下操作:
q = malloc(sizeof(struct node));
but the scope of q
is limited to addNode()
. You should have declared addNode()
as
但q的范围仅限于addNode()。您应该将addNode()声明为
void addNode(struct node **q, char *d)
and adjust your code accordingly:
并相应地调整您的代码:
*q = malloc(sizeof(struct node));
and so on...
等等...
#2
3
When you pass struct node *q
to addNode
you are giving it an address for an element in your array. If you use malloc
inside, then you are overwriting this variable q
, which is local to the function and now points to something different, but you haven't changed your original array. Try using a pointer to pointer to node (struct node **q
).
当您将struct node * q传递给addNode时,您将为其提供数组中元素的地址。如果你在里面使用malloc,那么你将覆盖这个变量q,它是函数的本地变量,现在指向不同的变量,但你还没有改变原始数组。尝试使用指向节点的指针(struct node ** q)。
#3
2
void addNode(struct node *q, char *d){
if(q == NULL)
q = malloc(sizeof(struct node));
Here's the problem.
这是问题所在。
The new value of q
doesn't ever get out of the function, so your array of linked lists never gets updated.
q的新值永远不会离开函数,因此链接列表的数组永远不会更新。
Normally the solution here is to use a double-pointer:
通常这里的解决方案是使用双指针:
void addNode(struct node **q, char *d){
if(*q == NULL)
*q = malloc(sizeof(struct node));
And call it like so:
并称之为:
addNode(&nodesArr[i],word);
Then, if you malloc
a new node, the value in the array will be set to point to the new node.
然后,如果malloc新节点,则数组中的值将设置为指向新节点。
#4
-2
struct node
{
int actual, estimated;
char c;
struct node *next;
} *head[4], *var[4], *trav[4];
void
insert_at_end (char c, int value, int value1)
{
struct node *temp;
temp = head[i];
var[i] = (struct node *) malloc (sizeof (struct node));
var[i]->actual = value;
//var1=(struct node *)malloc(sizeof(struct node));
var[i]->estimated = value1;
var[i]->c = c;
//printf("%d",var->estimated);
if (head[i] == NULL)
{
head[i] = var[i];
head[i]->next = NULL;
}
else
{
while (temp->next != NULL)
{
temp = temp->next;
}
var[i]->next = NULL;
temp->next = var[i];
}
}
#1
5
The problem lies in addNode()
. When the list is empty you do:
问题在于addNode()。当列表为空时,您执行以下操作:
q = malloc(sizeof(struct node));
but the scope of q
is limited to addNode()
. You should have declared addNode()
as
但q的范围仅限于addNode()。您应该将addNode()声明为
void addNode(struct node **q, char *d)
and adjust your code accordingly:
并相应地调整您的代码:
*q = malloc(sizeof(struct node));
and so on...
等等...
#2
3
When you pass struct node *q
to addNode
you are giving it an address for an element in your array. If you use malloc
inside, then you are overwriting this variable q
, which is local to the function and now points to something different, but you haven't changed your original array. Try using a pointer to pointer to node (struct node **q
).
当您将struct node * q传递给addNode时,您将为其提供数组中元素的地址。如果你在里面使用malloc,那么你将覆盖这个变量q,它是函数的本地变量,现在指向不同的变量,但你还没有改变原始数组。尝试使用指向节点的指针(struct node ** q)。
#3
2
void addNode(struct node *q, char *d){
if(q == NULL)
q = malloc(sizeof(struct node));
Here's the problem.
这是问题所在。
The new value of q
doesn't ever get out of the function, so your array of linked lists never gets updated.
q的新值永远不会离开函数,因此链接列表的数组永远不会更新。
Normally the solution here is to use a double-pointer:
通常这里的解决方案是使用双指针:
void addNode(struct node **q, char *d){
if(*q == NULL)
*q = malloc(sizeof(struct node));
And call it like so:
并称之为:
addNode(&nodesArr[i],word);
Then, if you malloc
a new node, the value in the array will be set to point to the new node.
然后,如果malloc新节点,则数组中的值将设置为指向新节点。
#4
-2
struct node
{
int actual, estimated;
char c;
struct node *next;
} *head[4], *var[4], *trav[4];
void
insert_at_end (char c, int value, int value1)
{
struct node *temp;
temp = head[i];
var[i] = (struct node *) malloc (sizeof (struct node));
var[i]->actual = value;
//var1=(struct node *)malloc(sizeof(struct node));
var[i]->estimated = value1;
var[i]->c = c;
//printf("%d",var->estimated);
if (head[i] == NULL)
{
head[i] = var[i];
head[i]->next = NULL;
}
else
{
while (temp->next != NULL)
{
temp = temp->next;
}
var[i]->next = NULL;
temp->next = var[i];
}
}