处理链表数组
我的方法:
一个固定长度的数组(比如说 20),每个元素都是指向链表第一个节点的指针。 所以我有20个不同的链表。
这是结构:
struct node{
char data[16];
struct node *next;
};
我现在对该数组的声明
struct node *nodesArr[20];
将一个新节点添加到链表之一,我这样做:
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")
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;
}
并从链表数组打印数据,我这样做:
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;
}
}
}
现在编译器没有给出错误,程序运行,我将数据传递给它,当我调用 print 时,它不打印任何东西,,?
更新::
在我编辑代码后(谢谢你),我认为问题出在打印功能上,有什么想法吗?
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.
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:
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题出在
addNode()
。当列表为空时,您会执行以下操作:但是
q
的范围仅限于addNode()
。您应该将addNode()
声明为并相应地调整您的代码:
依此类推...
The problem lies in
addNode()
. When the list is empty you do:but the scope of
q
is limited toaddNode()
. You should have declaredaddNode()
asand adjust your code accordingly:
and so on...
当您将 struct node *q 传递给 addNode 时,您就为它提供了数组中元素的地址。如果您在内部使用
malloc
,那么您将覆盖这个变量q
,它是函数的本地变量,现在指向不同的东西,但您没有更改原始数组。尝试使用指向节点的指针(struct node **q
)。When you pass
struct node *q
toaddNode
you are giving it an address for an element in your array. If you usemalloc
inside, then you are overwriting this variableq
, 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
).问题就在这里。
q
的新值永远不会离开该函数,因此您的链表数组永远不会更新。通常,这里的解决方案是使用双指针:
并像这样调用它:
然后,如果您
malloc
一个新节点,数组中的值将被设置为指向新节点。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.Normally the solution here is to use a double-pointer:
And call it like so:
Then, if you
malloc
a new node, the value in the array will be set to point to the new node.