简体   繁体   English

打印LinkedList时程序崩溃

[英]Program crashes when printing LinkedList

I have a double linked list in C, which's nodes hold a char*. 我在C中有一个双链表,其中的节点都包含一个char *。 My struct for the node looks like this: 我的节点结构如下所示:

struct LinkedListNode{
char* data;
LinkedListNode* next;
LinkedListNode* prev; };

The struct for the LinkedList looks like this: LinkedList的结构如下所示:

struct LinkedList{
LinkedListNode* head;
LinkedListNode* tail;};

head is a pointer to the first node of the list, tail is a pointer to the last node of the list. head是指向列表的第一个节点的指针,tail是指向列表的最后一个节点的指针。 My problem is that I am trying to write a function for testing some standard functions for linked lists, which I am implementing for practice purposes. 我的问题是,我正在尝试编写一个用于测试链表的某些标准功能的函数,出于实践目的,我将其实现。 So I start by generating a list in the following function: 因此,我首先在以下函数中生成一个列表:

LinkedList* make_test_list(){
LinkedList* newlist = LinkedList_create();
printf("Hier2");
LinkedListNode n1;
LinkedListNode n2;
LinkedListNode n3;
LinkedListNode n4;
LinkedListNode n5;
n1.data = "abc";
n2.data = "def";
n3.data = "ghi";
n4.data = "pqr";
n5.data = "mno";
n1.next = &n2;
n2.next = &n3;
n2.prev = &n1;
n3.next = &n4;
n3.prev = &n2;
n4.next = &n5;
n4.prev = &n3;
n5.prev = &n4;
n5.next = NULL;
newlist->head = &n1;
newlist->tail = &n5;
return newlist;}

Which appears to be working fine, since I can reaccess the data of each node, if I try to print it inside of this function. 如果我尝试在此函数中打印数据,则可以重新访问每个节点的数据,因此似乎工作正常。 LinkedList_create() contains the following code: LinkedList_create()包含以下代码:

LinkedList* LinkedList_create(){
LinkedList* list = malloc(sizeof(struct LinkedList));
list->head = NULL;
list->tail = NULL;
return list;
}

So the next thing I want to do is printing my list in a seperate function. 因此,我接下来要做的是在单独的函数中打印列表。 The function looks like this: 该函数如下所示:

void LinkedList_print(LinkedList* list){    
LinkedListNode* p = list->head;
while(p != NULL)){
    printf("%s\n", p->data);
    p = p->next;
}}

But somehow it doesn't work. 但是不知何故,它不起作用。 I think that I made a mistake while assigning my pointers. 我认为分配指针时犯了一个错误。 The call looks like this: 呼叫看起来像这样:

int main(){
LinkedList* myList = make_test_list();
printf("List before: \n");

LinkedList_print(myList);
}

I'd be glad if you could help me out on this one, since I'm quite new to C and new to managing memory allocation by myself. 如果您能帮上我的忙,我将感到非常高兴,因为我对C还是陌生的,而且对我自己管理内存分配还很陌生。

Cheers! 干杯!

Your LinkedListNode variables ( n1 - n5 ) are local to the make_test_list function. 您的LinkedListNode变量( n1 - n5 )是make_test_list函数的本地变量。 You need to allocate memory for them if you want to access them outside of that function. 如果要在该功能之外访问它们,则需要为其分配内存。

For example: 例如:

LinkedListNode *n1 = malloc(sizeof(*n1));
n1->data = "abc";
...
newlist->head = n1;
...

Of course, you also want to make sure you free the memory later. 当然,您还希望确保以后free内存。 I am also making the assumption that LinkedList_create is implemented correctly, since it isn't shown here. 我还假设LinkedList_create已正确实现,因为此处未显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM