[英]Adding a node to a Linked List gives a runtime error in C++
I'm trying to create a function that will add a node at the end of a linked list. 我正在尝试创建一个函数,该函数将在链表的末尾添加一个节点。 Every time I run it however, I'm getting a runtime error and have no idea where my mistake is.
但是,每次运行它时,都会遇到运行时错误,并且不知道我的错误在哪里。 Here's my code:
这是我的代码:
Edge* append(Edge *head, int origin, int destination, int weight) {
Edge *temp = new Edge;
temp = head;
Edge *node = new Edge;
while (temp->next != NULL){
temp = temp->next;
}
node->vert1 = origin;
node->vert2 = destination;
node->weight = weight;
node->next = NULL;
if (head == 0) {
head = node;
}
else if (head != 0){
node = temp;
}
return head;
}
UPDATE: 更新:
Here is the Edge struct: 这是Edge结构:
struct Edge {
int vert1;
int vert2;
int weight;
Edge *next;
};
Here is the main function: 这是主要功能:
int main(){
Edge *e = append(NULL, 1,4,5);
}
First thing I notice is that you check if head is NULL after you already dereferenced it. 我注意到的第一件事是, 在已经取消引用head 之后 ,您检查它是否为NULL。 You set temp to head and then check if temp->next is NULL.
您将temp设置为head,然后检查temp-> next是否为NULL。 If head was NULL this crashes.
如果head为NULL,则崩溃。 Furthermore your else if is redundant.
此外,您的else是否多余。 If head was not equal to 0 than it must be different from 0. The third thing is that at the end you set node to temp but I think what you want to do is to set temp->next to node.
如果head不等于0,那么它必须不同于0。第三件事是,最后将node设置为temp,但是我认为您想要做的就是将temp-> next设置为node。
Edge* append(Edge *head, const int origin, const int destination, const int weight) {
Edge *temp = head;
Edge *node = new Edge;
node->vert1 = origin;
node->vert2 = destination;
node->weight = weight;
node->next = NULL;
if (!head) {
return node;
}
while (temp->next != NULL){
temp = temp->next;
}
temp->next = node;
return head;
}
EDIT Your code has a memmory leak, too. 编辑您的代码也有内存泄漏。 You never free the memory that you reserved for temp at the first line.
您永远不会在第一行中释放为temp保留的内存。 However, there is no need to create a new Edge here.
但是,无需在此处创建新的Edge。
while you declare: 当您声明:
Edge *temp = new Edge;
You then reassign temp to head: 然后,您重新分配临时负责人:
temp = head;
Therefore, if you pass in a null head
param, you will crash at 因此,如果你在一个空传
head
PARAM,你会崩溃
while (temp->next != NULL){
It seems like this reassignment isn't what you're actually looking to do, since the logic doesn't work out anyways beyond that. 似乎这种重新分配并不是您真正想要做的,因为除此之外,逻辑还没有奏效。 Try this:
尝试这个:
Edge* append(Edge *head, int origin, int destination, int weight) {
// create your new node
Edge *node = new Edge;
node->vert1 = origin;
node->vert2 = destination;
node->weight = weight;
node->next = NULL;
// if there is no head, this is the head
if (head == NULL) {
head = node;
return node;
}
// else walk to the end and set this as as the new tail node
// NOTE: this can be optimized by having your linkedlist implementation
// store the pointer to the tail.
Edge *temp = head;
while (temp->next != NULL){
temp = temp->next;
}
temp->next = node;
return node;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.