用C语言实现链表数组
我正在尝试用下面的数据集实现动态链表用C语言实现链表数组,c,linked-list,singly-linked-list,C,Linked List,Singly Linked List,我正在尝试用下面的数据集实现动态链表 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 &&
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();
}
}
现在我不知道如何修复此代码以保留每个项目的正确密钥
请帮忙
谢谢,
Kunjal至少需要添加指向同一结构类型的指针,以将所有节点链接到一个适当的列表中。差不多
typedef struct node {
uint8_t item1;
uint8_t item2;
char *key;
struct node *next;
} node;
请注意,我已经重命名了用typedef
给出的最终名称,因为以\u t
结尾的类型名称实际上是为将来的语言添加保留的(read)。这同样适用于以开头的名字,正如比尔在下面的评论中所说的那样。答案如下
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();
}
}
typedef结构节点{
uint8_t第1项;
uint8_t第2项;
字符*键;
结构节点*下一步;
}节点;
node*nodes=NULL;
静态节点*find_by_item1(uint8_t item1){
node*sl=节点;
而(sl&&sl->item1!=item1)
sl=sl->next;
返回sl;
}
void createNode(char*键、uint8\u t项1、uint8\u t项2){
node*sl=find_by_item1(item1);
if(sl){
//如果已再次输入密钥,请执行进一步处理
返回;
}
node*sl=malloc(sizeof(node));
如果(!(sl=malloc(strlen(键)+1))){
免费(sl);
返回;
}
/*
根据一些阅读资料添加,其中一些专家建议使用该指针的内存区域,以允许它保留该值
*/
sl->key=malloc(sizeof(*key));
//添加数据
sl->item1=item1;
sl->item2=item2;
strcpy(sl->key,key);
sl->next=节点;
节点=sl;
}
void printNode(){
节点*sl;
对于(sl=节点;NULL!=sl;sl=sl->next){
printf(“\n节点%s%d%d”,sl->Key,sl->item1,sl->item2);
}
}
void main(){
对于(uint8_t i=1;我请说明您到底尝试了什么,以及您的实现失败了,因此我们可以从中构建。“已经花了大约3-4天的时间不断思考和实现”-您是否尝试在谷歌上搜索“c链表”?@klutt是的,标题上写着“链表数组”让我觉得他还没有完全理解链表到底是什么。或者,他可能是在试图根据他之前对JS或Python等语言中的列表和数组的了解来实现它们,而这些语言中的列表和数组似乎是相同的。下面是一些示例代码if(sl)return
将始终在sl
不为空时返回。我想如果(!sl)return
还保留了一个前导下划线,您可能需要。
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();
}
}