2009-11-28 36 views
1

我试图让链接类似的列表太这里的一个:C++链表

linked list in C

这是有“头”,我第一次把它称为,另一个结构里面。不过,我发现正在做这种改变。很难将值添加到list_item结构中。我已经尝试了几件事情,看它是否有效。它编译,但是当我运行代码时它会崩溃。任何帮助在这里都会有所帮助。我知道崩溃的原因是我想将new_node指向linked_list。

#include <iostream> 

using namespace std; 

struct list_item 
{ 
    int key; 
    int value; 
    list_item *next; 
}; 

struct list 
{ 
    struct list_item *first; 
}; 

int main() 
{ 
    list *head; 
    list *new_node; 

    head = NULL; 
    head->first = NULL; 

    for(int i = 0; i < 10; i++) 
    { 
     //allocate memory for new_node 
     new_node = (list*)malloc(sizeof(list)); 
     new_node->first = (list_item*)malloc(sizeof(list_item)); 
     //adding the values 
     new_node->first->key = i; 
     new_node->first->value = 10 + i; 

     //point new_node to first; 
     new_node->first->next = head->first; 

     //point first to new_node; 
     head->first = new_node->first; 

    } 

    //print 
    list *travel; 
    travel->first = head->first; 

    int i = 0; 
    while(travel != NULL) 
    { 
     cout << travel->first->value << endl; 
     travel->first = travel->first->next; 
    } 

    return 0; 
} 
+0

请注意,C++已将std :: list <>作为标准容器提供给您。出于这个原因,我删除了你的C++标签。 – ChrisInEdmonton 2009-11-28 16:43:26

+0

如果它将被标记为C而不是C++,请使用结构而不是类,而使用malloc而不是运算符new,我很想编辑它以开始使用printf,停止投射malloc的返回值,而不依赖于implicit typedef from struct definition:P – asveikau 2009-11-28 16:49:53

回答

5

你正在创建10个名单,我想你可以尝试做一个示范)

见Gabe的职位;你分配一个列表指针到只接受LIST_ITEM指针成员像这样:

#include <iostream> 

using namespace std; 

struct list_item 
{ 
    int key; 
    int value; 
    list_item *next; 
}; 

struct list 
{ 
    struct list_item *first; 
}; 

int main() 
{ 
    //Just one head is needed, you can also create this 
    // on the stack just write: 
    //list head; 
    //head.first = NULL; 
    list *head = (list*)malloc(sizeof(list)); 
    list_item *new_node = NULL; 

    head->first = NULL; 

    for(int i = 0; i < 10; i++) 
    { 
     //allocate memory for new_node 
     new_node = (list_item*)malloc(sizeof(list_item)); 
     //adding the values 
     new_node->key = i; 
     new_node->value = 10 + i; 

     //if the list is empty, the element you are inserting 
     //doesn't have a next element 

     new_node->next = head->first; 

     //point first to new_node. This will result in a LIFO 
     //(Last in First out) behaviour. You can see that when you 
     //compile 
     head->first = new_node; 

    } 

    //print the list 
    list_item *travel; 
    travel = head->first; 

    while(travel != NULL) 
    { 
     cout << travel->value << endl; 
     travel = travel->next; 
    } 

    //here it doesn't matter, but in general you should also make 
    //sure to free the elements 
    return 0; 
} 

这是发生了什么事情。起初你只有一个头,没有元素。

head 
    | 
    | 
    V 
NULL 

然后你添加你的第一个元素。确保“new_node-> next == NULL”:

head 
    | 
    | 
    V 
node: ------------------> NULL 
key = 0 
value = 10 

然后在前面添加另一个节点,但将第一个节点追加到其下一个节点。你从头部将指针移动到新节点

head: 
first 
    | 
    | 
    V 
node: ---------> node: -------------> NULL 
key: 1    key: 0 
value: 11   value: 10 

由于您使用C++,你可以考虑使用“新”和“删除”。与

list *head = new list 
+0

嘿,我正在通过代码。试图理解它。感觉非常不喜欢,但我似乎没有找到为什么循环内的if语句在列表末尾删除NULL的原因如果(head-> first!= NULL) 如果在那里旅行循环将会崩溃。 – starcorn 2009-11-28 16:12:09

+0

@klw:我刚刚意识到你的意思。当然,你是100%正确的。由于head-> first已经指向NULL,所以if ... else ...完全没用。 – Lucas 2009-11-28 16:39:56

1

下一行只为您的list结构分配内存。该列表仅包含一个指针,您还必须在分配给其任何成员之前为new_node->first分配内存。

//allocate memory for new_node 
new_node = (list*)malloc(sizeof(list)); 
+0

我现在为它添加了malloc,但它仍然崩溃。我还想念什么?我更新了代码 – starcorn 2009-11-28 15:11:27

2

我想你想要更多的东西是这样的:

注意
#include <iostream> 
#include <cstdlib> 

using namespace std; 

typedef struct tag_list_item 
{ 
    int key; 
    int value; 
    struct tag_list_item *next; 
} list_item; 

typedef struct 
{ 
    list_item *head; 
} list; 

int main() 
{ 
    list my_list; 
    list_item *new_node; 
    list_item *previous_node = NULL; 

    my_list.head = NULL; 

    for(int i = 0; i < 10; i++) 
    { 
     //allocate memory for new_node 
     new_node = (list_item*)malloc(sizeof(list_item)); 

     //adding the values 
     new_node->key = i; 
     new_node->value = 10 + i; 

     if(previous_node == NULL) 
     { 
      my_list.head = new_node; 
     } 
     else 
     { 
      previous_node->next = new_node; 
     } 
     previous_node = new_node;  
    } 

    //print 
    list_item *iter = my_list.head; 

    while(iter != NULL) 
    { 
     cout << iter->value << endl; 
     iter = iter->next; 
    } 

    return 0; 
} 

变化:

对于malloc的,我说:

#include <cstdlib> 

我改变了你的列表结构typedef,必须使用标签声明“next”,因为typedef在该点不完整

typedef struct tag_list_item 
{ 
    int key; 
    int value; 
    struct tag_list_item *next; 
} list_item; 

我将您的列表名称更改为“my_list”并直接声明它(不带指针)。在这种情况下,您可以让编译器自动在堆栈中分配它。

list my_list; 

我保持一个指针“previous_node”,让你可以指定“下一个”更容易指针。请注意,分配的第一个节点是由列表结构中的“头”指针指向的。我相信这是指向列表中第一个元素的传统名称。

if(previous_node == NULL) 
{ 
    my_list.head = new_node; 
} 
else 
{ 
    previous_node->next = new_node; 
} 
previous_node = new_node; 
0
head = NULL; 
head->first = NULL; 

有问题。如果将指针设置为NULL,则不能跟随指针并将其设置为NULL。

这应该是

head = malloc(sizeof(list)); 
head->first = NULL; 

这应该解决您的代码。

希望帮助, Billy3

编辑:还有一个问题,您的for循环。当你分配清单时,你应该只分配清单本身一次。当你插入一个项目时,你只分配一个list_item。对于正确的行为:)

-1

看看你的结构声明只需更换

new_node = (list_item*)malloc(sizeof(list_item)); 

 
struct list_item 
{ 
    int key; 
    int value; 
    list_item *next; 
}; 

这应该是

 
struct list_item 
{ 
    int key; 
    int value; 
    struct list_item *next; 
}; 

希望这有助于 最好的问候, Tom

+1

不在C++中。这在C中是正确的。 OP的问题不是编译时问题,而是运行时间分段错误。 – 2009-11-29 07:33:05