如何创建单链表数组
How to create an array of singly linked list
我正在尝试创建一个链表数组,其中每个列表的节点都是字母表中的一个字符,因此该数组应该有 26 个元素。我试图通过给每个元素的第一个节点字符串“。”来做到这一点。然后使用插入
这是我的链表定义
struct Node{
char *name;
struct Node *next;
};
void printList(struct Node *node)
{ printf("[");
while (node !=NULL){
printf("%s,", node->name);
node = node->next;
}
printf("]\n");
}
struct Node *current = NULL;
void append(struct Node* head, char* new_data)
{
/* 1. allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->name = new_data;
new_node->next = NULL;
if(head == NULL){
head = new_node;
return;
}
else{
while(head->next != NULL){
head = head->next;
}
head->next = new_node;
return;
}
}
这里是主函数,在其中,我创建了一个字母表的字符串al,然后使用for循环更新数组中的每个链表。
int main(){
struct Node list[26];
for(int x=0; x<26; x++){
struct Node* first = (struct Node*) malloc(sizeof(struct Node));
first->name = ".";
first->next= NULL;
list[x] = *first;
}
char *al = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int x = 0; x<26; x++){
char name[2];
name[0] = al[x];
name[1] = '[=11=]';
printf("%s\n", name);
append(&list[x], name);
}
return 0;
}
循环后,LinkedList的name值全部为Z,而不是从A到Z。
*编辑
我打印数组内每个链表的第二个节点
for(int n =0; n<26; n++){
printf("%s\n", list[n].next->name);
}
这是结果
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
它应该是从 A 到 Z 而不仅仅是 Z。
替换:
struct Node list[26]
与 struct Node *list[26]
list[x] = *first
与 list[x] = first
append(&list[x], name)
与 append(list[x], name)
struct Node list[26]
是一个 struct Node
的数组,但是你想要一个 指向 struct Node
的指针数组 ,数组的每个元素都是指向列表头部的指针。
奖励:您的 append
功能非常低效。为了将一个元素附加到列表中,您必须遍历整个列表以找到最后一个元素。您应该维护指向列表最后一个元素的指针。
我正在尝试创建一个链表数组,其中每个列表的节点都是字母表中的一个字符,因此该数组应该有 26 个元素。我试图通过给每个元素的第一个节点字符串“。”来做到这一点。然后使用插入
这是我的链表定义
struct Node{
char *name;
struct Node *next;
};
void printList(struct Node *node)
{ printf("[");
while (node !=NULL){
printf("%s,", node->name);
node = node->next;
}
printf("]\n");
}
struct Node *current = NULL;
void append(struct Node* head, char* new_data)
{
/* 1. allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->name = new_data;
new_node->next = NULL;
if(head == NULL){
head = new_node;
return;
}
else{
while(head->next != NULL){
head = head->next;
}
head->next = new_node;
return;
}
}
这里是主函数,在其中,我创建了一个字母表的字符串al,然后使用for循环更新数组中的每个链表。
int main(){
struct Node list[26];
for(int x=0; x<26; x++){
struct Node* first = (struct Node*) malloc(sizeof(struct Node));
first->name = ".";
first->next= NULL;
list[x] = *first;
}
char *al = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int x = 0; x<26; x++){
char name[2];
name[0] = al[x];
name[1] = '[=11=]';
printf("%s\n", name);
append(&list[x], name);
}
return 0;
}
循环后,LinkedList的name值全部为Z,而不是从A到Z。
*编辑 我打印数组内每个链表的第二个节点
for(int n =0; n<26; n++){
printf("%s\n", list[n].next->name);
}
这是结果
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
Z
它应该是从 A 到 Z 而不仅仅是 Z。
替换:
struct Node list[26]
与struct Node *list[26]
list[x] = *first
与list[x] = first
append(&list[x], name)
与append(list[x], name)
struct Node list[26]
是一个 struct Node
的数组,但是你想要一个 指向 struct Node
的指针数组 ,数组的每个元素都是指向列表头部的指针。
奖励:您的 append
功能非常低效。为了将一个元素附加到列表中,您必须遍历整个列表以找到最后一个元素。您应该维护指向列表最后一个元素的指针。