[英]Implement linked-list array in C
I am trying to implement the dynamic linked list with the below dataset.我正在尝试使用以下数据集实现动态链表。
typedef struct node {
uint8_t item1;
uint8_t item2;
char* key;
struct node *next;
} node;
node *nodes = NULL;
static node* find_by_item1(uint8_t item1) {
node *sl = nodes;
while(sl && sl->item1 != item1)
sl = sl->next;
return sl;
}
void createNode(char* key, uint8_t item1, uint8_t item2){
node *sl = find_by_item1(item1);
if(sl){
//Do Process further if the key is already entered again
return;
}
node *sl = malloc(sizeof(node));
if(!(sl = malloc(strlen(key)+1))){
free (sl);
return;
}
//memset(sl, 0, sizeof(*sl));
//Add the data
sl->item1 = item1;
sl->item2 = item2;
strcpy (sl->key, key);
sl->next = nodes;
nodes = sl;
}
void printNode(){
Node *sl;
for(sl = nodes; NULL != sl; sl = sl->next){
printf("\nNode %s %d %d", sl->Key, sl->item1, sl->item2);
}
}
void main(){
for (uint8_t i = 1; i <= 4; i++) {
char key_name[12] = {0};
sprintf(key_name, “Key-%d”, i);
switch (i) {
case 1:
createNode(key_name, 1, 2);
break;
case 2:
createNode(key_name, 3, 4);
break;
case 3:
createNode(key_name, 5, 6);
break;
case 4:
createNode(key_name, 7, 8);
break;
default:
break;
}
}
printNode();
}
}
I have tried to implement this based on my research but somehow unable to achieve the result that I was intending.我试图根据我的研究来实现这一点,但不知何故无法达到我想要的结果。 Have spent about last 3-4 days constantly thinking and implementing and re-implementing this but I seems to be missing something obvious.
在过去的 3-4 天里一直在思考、实施和重新实施这一点,但我似乎遗漏了一些明显的东西。
My program fails at 1st line in "find_by_item1" while(sl && sl->item1 != item1)
我的程序在“find_by_item1”的第一行失败
while(sl && sl->item1 != item1)
Any pointers will be helpful.任何指针都会有所帮助。
---Update - -更新
I have been trying to understand the absurd error and seems the error was something related to the NULL Pointer Reference.我一直在试图理解这个荒谬的错误,似乎该错误与 NULL 指针参考有关。
Commenting the line memset(sl, 0, sizeof(*sl));
注释行
memset(sl, 0, sizeof(*sl));
allowed to start making the progress again.允许再次开始取得进展。 However, now, when I try to print the link list below is the input and output:
但是,现在,当我尝试打印下面的链接列表时,输入和 output:
Input:
Key-1 1 2
Key-2 3 4
Key-3 5 6
Key-4 7 8
Output:
Node Key-4 1 2
Node Key-4 3 4
Node Key-4 5 6
Node Key-4 7 8
Now I am not sure how can I fix this code to retain the correct Key against each items.现在我不确定如何修复此代码以针对每个项目保留正确的密钥。
Help pls.请帮忙。
Thanks, Kunjal谢谢,昆贾尔
At the very minimum you need to add a pointer to the same struct type to chain together all the nodes into a proper list.至少您需要添加一个指向相同结构类型的指针,以将所有节点链接到一个适当的列表中。 Something like
就像是
typedef struct node {
uint8_t item1;
uint8_t item2;
char *key;
struct node *next;
} node;
Notice that I have renamed the final name given with the typedef
because type names that end in _t
are actually reserved for future language additions (read here ).请注意,我已重命名使用
typedef
给出的最终名称,因为以_t
结尾的类型名称实际上是为将来的语言添加保留的(请阅读此处)。 The same also applies to names starting with _
, as said by Bill in the comment below this answer.正如比尔在此答案下方的评论中所说,这同样适用于以
_
开头的名称。
Here's the answer.这是答案。
typedef struct node {
uint8_t item1;
uint8_t item2;
char* key;
struct node *next;
} node;
node *nodes = NULL;
static node* find_by_item1(uint8_t item1) {
node *sl = nodes;
while(sl && sl->item1 != item1)
sl = sl->next;
return sl;
}
void createNode(char* key, uint8_t item1, uint8_t item2){
node *sl = find_by_item1(item1);
if(sl){
//Do Process further if the key is already entered again
return;
}
node *sl = malloc(sizeof(node));
if(!(sl = malloc(strlen(key)+1))){
free (sl);
return;
}
/*
Added based on some reading where some experts suggested to have this pointer's memory area which will allow it to retain the value
*/
sl->key = malloc(sizeof(*key));
//Add the data
sl->item1 = item1;
sl->item2 = item2;
strcpy (sl->key, key);
sl->next = nodes;
nodes = sl;
}
void printNode(){
Node *sl;
for(sl = nodes; NULL != sl; sl = sl->next){
printf("\nNode %s %d %d", sl->Key, sl->item1, sl->item2);
}
}
void main(){
for (uint8_t i = 1; i <= 4; i++) {
char key_name[12] = {0};
sprintf(key_name, “Key-%d”, i);
switch (i) {
case 1:
createNode(key_name, 1, 2);
break;
case 2:
createNode(key_name, 3, 4);
break;
case 3:
createNode(key_name, 5, 6);
break;
case 4:
createNode(key_name, 7, 8);
break;
default:
break;
}
}
printNode();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.