我是一个Noob,在C语言,指针和链接列表中苦苦挣扎(嗯,确实是一切)。我正在尝试构建一个非常简单的链表链表,但收到的错误消息是“成员引用基类型” struct node *”不是结构或联合。经过一阵苦苦挣扎,我突然意识到,列表列表中的下一个都可以指向两个地方的下一个列表项,但是不确定我是否需要两种类型的节点,或者哪种更好的方法呢?
li ul{
list-style-type:circle;
}
答案 0 :(得分:3)
您的list_1和list_2被定义为指向该节点的指针的指针。 您真正想要的是list_1和list_2成为一个节点,但它们的数据成为指向其他节点的指针。由于您定义的节点结构已将数据定义为字符指针,因此当前节点将不适用于list_1和list_2。您可以更改结构以使用void-pointer,也可以创建用于保存节点的新结构。 较简单的解决方案是使用新结构。
#include <stdio.h>
#include <stdlib.h>
// node that can hold char * (strings) and next
struct node {
char* data;
struct node * next;
};
// node that can hold lists of nodes and next
struct lnode {
struct node* data;
struct lnode * next;
};
/*
I'm trying to create a linked list (containing List 1 and List 2) of linked lists, i.e.
List 1 List 2
1A 2A
1B 2B
Where only nodes 1A, 1B, 2A and 2B have unique data (List 1 data just pointers to 1A data, List1 next just points to 1A next etc.)
*/
int main()
{
// Will contain pointers to the heads of the 2 linked lists
struct lnode* list_1 = NULL;
struct lnode* list_2 = NULL;
// Nodes under list_1
struct node* one_a = NULL;
struct node* one_b = NULL;
// Nodes under linked list b
struct node* two_a = NULL;
struct node* two_b = NULL;
// allocate 6 nodes in the heap
list_1 = malloc(sizeof *list_1); //This style of malloc is better in the C language
list_2 = malloc(sizeof *list_2);
// list 1 will contain
one_a = malloc(sizeof *one_a);
one_b = malloc(sizeof *one_b);
// list 2 will contain
two_a = malloc(sizeof *two_a);
two_b = malloc(sizeof *two_b);
// create linked list holding heads of 2 linked lists (i.e. first_list and second_list)
// populate list1
one_a->data = "a";
one_a->next = one_b;
one_b->data = "apple";
one_b->next = NULL;
// populate list2
two_a->data = "be";
two_a->next = two_b;
two_b->data = "bite";
two_b->next = NULL;
// populate list of lists
list_1->data = one_a;//one_a is a pointer to the list
list_1->next = list_2;
list_2->data = two_a; //two_a is a pointer to the list
list_2->next = NULL;
}
答案 1 :(得分:1)
仅使用该结构无法实现您要执行的操作。您至少需要两个指针,一个用于列表列表的元素,另一个用于char*
列表。
您可以使用的最接近此处的代码的方式是使用以下列表数组:
list_1 = (struct node**)malloc(sizeof(struct node*) * SIZE);
为了实现您想要的目标,您需要某种如下的结构:
struct node{
char* data;
node* next;
};
struct list{
list* next;
node* curr_list;
};
如您所见,现在您有一个用于列表列表的特殊节点,该节点既指向列表元素的下一个列表,又指向字符串的 normal 列表。
以下是带有main
的完整示例代码。
您可以here尝试一下。
#include <stdio.h>
#include <stdlib.h>
// node that can hold char * (strings) and next
struct node{
char* data;
struct node* next;
};
struct list{
struct list* next;
struct node* curr_list;
};
int main(){
struct node node2;
node2.data="second";
node2.next=NULL;
struct node node1;
node1.data="first";
node1.next=&node2;
struct list l1;
l1.next=NULL;
l1.curr_list= &node1;
struct node node4;
node4.data="fourth";
node4.next=NULL;
struct node node3;
node3.data="third";
node3.next= &node4;
struct list l2;
l2.next=NULL;
l2.curr_list= &node3;
l1.next = &l2;
struct list* pl = &l1;
int i = 0;
while(pl){
printf("%s %d\n","list",i++);
struct node* n = pl->curr_list;
while(n){
printf("%s\n",n->data);
n=n->next;
}
pl = pl->next;
printf("\n");
}
return 0;
}
答案 2 :(得分:0)
使用亚当的答案(以及戴维(Davide)关于打印功能的逻辑),我得到了下面的C语言中简单链接列表的演示:
#include <stdio.h>
#include <stdlib.h>
// node that can hold char * (strings) and next
struct node {
char* data;
struct node * next;
};
// node that can hold lists of lnodes and next
struct lnode {
struct node* data;
struct lnode * next;
};
/*
I'm trying to create a linked list (containing List 1 and List 2) of linked lists, i.e.
List 1 List 2
1A 2A
1B 2B
Where only nodes 1A, 1B, 2A and 2B have unique data (List 1 data just pointers to 1A data, List1 next just points to 1A next etc.)
*/
int main()
{
// Will contain pointers to the heads of the 2 linked lists
struct lnode* list_1 = NULL;
struct lnode* list_2 = NULL;
// Nodes under list_1
struct node* one_a = NULL;
struct node* one_b = NULL;
// Nodes under linked list b
struct node* two_a = NULL;
struct node* two_b = NULL;
// allocate 6 nodes in the heap
list_1 = malloc(sizeof *list_1); //This style of malloc is better in the C language
list_2 = malloc(sizeof *list_2);
// list 1 will contain
one_a = malloc(sizeof *one_a);
one_b = malloc(sizeof *one_b);
// list 2 will contain
two_a = malloc(sizeof *two_a);
two_b = malloc(sizeof *two_b);
// create linked list holding heads of 2 linked lists (i.e. first_list and second_list)
// populate list1
one_a->data = "a";
one_a->next = one_b;
one_b->data = "apple";
one_b->next = NULL;
// populate list2
two_a->data = "be";
two_a->next = two_b;
two_b->data = "bite";
two_b->next = NULL;
// populate list of lists
list_1->data = one_a;//one_a is a pointer to the list
list_1->next = list_2;
list_2->data = two_a; //two_a is a pointer to the list
list_2->next = NULL;
// to iterate over seperate lists (i.e. a's, b's, c's etc.)
struct lnode *lists = list_1;
while (lists){
// to iterate through individual lists,(i.e. 'a', 'apple' etc.)
struct node *head = lists->data;
while (head) {
printf("%s\n", head->data);
head = head->next;
}
lists = lists->next;
}
}