[英]C++ Linked list
I'm trying to make linked list similar too the one here: 我正在尝试使链接列表也与此处相似:
That is to have the "head", I called it first, inside another struct. 那就是要在另一个结构内拥有“头”,我首先称它为“头”。 However I found doing that change.
但是我发现进行了更改。 Makes it hard to add values to the list_item struct.
使得很难将值添加到list_item结构。 I have tried some few things to see if it works.
我已经尝试了一些方法来查看它是否有效。 It compiles, however when I run the code it will crash.
它可以编译,但是当我运行代码时它将崩溃。 Any help would be helpful here.
任何帮助将对您有所帮助。 I know the cause of the crash is when I want to point the new_node to the linked_list.
我知道崩溃的原因是当我想将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;
}
You are creating 10 lists, I think you might try to do something like this: 您正在创建10个列表,我想您可能会尝试执行以下操作:
#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;
}
This is what is going on. 这是怎么回事。 At first you only have one head and no elements.
起初,您只有一个头,没有任何元素。
head
|
|
V
NULL
Then you add your first element. 然后添加您的第一个元素。 Make sure that the "new_node->next==NULL":
确保“ new_node-> next == NULL”:
head
|
|
V
node: ------------------> NULL
key = 0
value = 10
Then you add another node in front but append your first node to its next node. 然后,您在前面添加另一个节点,但将第一个节点附加到其下一个节点。 you move the pointer from the head to the new node
您将指针从头部移到新节点
head:
first
|
|
V
node: ---------> node: -------------> NULL
key: 1 key: 0
value: 11 value: 10
etc. 等等
Since you are using c++, you might consider using "new" and "delete". 由于您使用的是c ++,因此您可以考虑使用“ new”和“ delete”。 Just replace
只需更换
new_node = (list_item*)malloc(sizeof(list_item));
with 与
list *head = new list
I think you want something more like this: 我想您想要更多类似这样的东西:
#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;
}
Changes of note: 音符变化:
For malloc, I added: 对于malloc,我添加了:
#include <cstdlib>
I changed your list structures to typedefs, had to declare "next" using the tag since the typedef isn't complete at that point 我将您的列表结构更改为typedef,由于该位置的typedef尚未完成,因此必须使用标记声明“ next”
typedef struct tag_list_item
{
int key;
int value;
struct tag_list_item *next;
} list_item;
I changed your list name to "my_list" and declared it directly (without the pointer). 我将您的列表名称更改为“ my_list”,并直接声明了它(没有指针)。 In this case you can just have the compiler allocate it automatically on the stack.
在这种情况下,您可以让编译器自动在堆栈上分配它。
list my_list;
I keep a pointer for "previous_node" so that you can assign the "next" pointer much more easily. 我保留了“ previous_node”的指针,以便您可以更轻松地分配“ next”指针。 Notice that the first node allocated is pointed to by the "head" pointer in the list structure.
注意,分配的第一个节点由列表结构中的“ head”指针指向。 I believe that is the traditional name for the pointer to the first element in a list.
我相信这是指向列表中第一个元素的指针的传统名称。
if(previous_node == NULL)
{
my_list.head = new_node;
}
else
{
previous_node->next = new_node;
}
previous_node = new_node;
The next line only allocates memory for your list
struct. 下一行仅为
list
结构分配内存。 The list contains only a pointer, you must also allocate memory for new_node->first
before assigning to any of its members. 该列表仅包含一个指针,在分配给它的任何成员之前,还必须为
new_node->first
分配内存。
//allocate memory for new_node
new_node = (list*)malloc(sizeof(list));
head = NULL;
head->first = NULL;
There's the issue. 有问题。 You can't follow a pointer and set it to NULL if you've set the pointer itself to NULL.
如果将指针本身设置为NULL,则不能跟随指针并将其设置为NULL。
That should be 那应该是
head = malloc(sizeof(list));
head->first = NULL;
That should fix your code. 那应该修复您的代码。
Hope that helps, Billy3 希望有帮助,比利3
EDIT: There's also an issue with your FOR loop. 编辑:您的FOR循环也存在问题。 When you allocate the list, you should only allocate the list itself once.
分配列表时,只应分配一次列表本身。 When you insert an item, you only allocate a list_item.
插入项目时,仅分配一个list_item。 You're assigning a list pointer to a member which accepts only a list_item pointer ;)
您正在将列表指针分配给仅接受list_item指针的成员;)
See Gabe's post for a demonstration of correct behavior :) 有关正确行为的演示,请参见Gabe的文章:)
Look at your struct declaration 看看你的结构声明
struct list_item { int key; int value; list_item *next; };
That should be 那应该是
struct list_item { int key; int value; struct list_item *next; };
Hope this helps, Best regards, Tom 希望这对您有所帮助,汤姆,谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.