在 C 中实现链表数组
Implement linked-list array in C
我正在尝试使用以下数据集实现动态 linked 列表。
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();
}
}
我已经尝试根据我的研究来实现这个,但不知何故无法达到我想要的结果。在过去的 3-4 天里,我一直在思考、实施和重新实施这一点,但我似乎遗漏了一些明显的东西。
我的程序在“find_by_item1”的第一行失败 while(sl && sl->item1 != item1)
任何指点都会有所帮助。
---更新
我一直在试图理解这个荒谬的错误,似乎这个错误与 NULL 指针引用有关。
注释行memset(sl, 0, sizeof(*sl));
允许再次开始取得进展。
但是,现在,当我尝试打印下面的 link 列表时,输入和输出是:
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
现在我不确定如何修复此代码以针对每个项目保留正确的密钥。
请帮忙
谢谢,
昆加尔
您至少需要添加一个指向相同结构类型的指针,以将所有节点链接到一个适当的列表中。像
typedef struct node {
uint8_t item1;
uint8_t item2;
char *key;
struct node *next;
} node;
请注意,我已经重命名了以 typedef
给出的最终名称,因为以 _t
结尾的类型名称实际上是为将来的语言添加保留的(阅读 here)。这同样适用于以 _
开头的名称,正如比尔在此答案下方的评论中所说。
答案在这里。
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();
}
}
我正在尝试使用以下数据集实现动态 linked 列表。
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();
}
}
我已经尝试根据我的研究来实现这个,但不知何故无法达到我想要的结果。在过去的 3-4 天里,我一直在思考、实施和重新实施这一点,但我似乎遗漏了一些明显的东西。
我的程序在“find_by_item1”的第一行失败 while(sl && sl->item1 != item1)
任何指点都会有所帮助。
---更新
我一直在试图理解这个荒谬的错误,似乎这个错误与 NULL 指针引用有关。
注释行memset(sl, 0, sizeof(*sl));
允许再次开始取得进展。
但是,现在,当我尝试打印下面的 link 列表时,输入和输出是:
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
现在我不确定如何修复此代码以针对每个项目保留正确的密钥。
请帮忙
谢谢, 昆加尔
您至少需要添加一个指向相同结构类型的指针,以将所有节点链接到一个适当的列表中。像
typedef struct node {
uint8_t item1;
uint8_t item2;
char *key;
struct node *next;
} node;
请注意,我已经重命名了以 typedef
给出的最终名称,因为以 _t
结尾的类型名称实际上是为将来的语言添加保留的(阅读 here)。这同样适用于以 _
开头的名称,正如比尔在此答案下方的评论中所说。
答案在这里。
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();
}
}