【发布时间】:2022-11-12 12:47:04
【问题描述】:
我试图弄清楚如何复制一个链表,在对 Vs 代码进行调试后,我在cuurent->data = temp->data; 上遇到了分段错误
我不确定为什么会这样。
这是代码:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* head;
struct node* head2;
struct node* Insert(struct node* head, int x)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = x;
temp->next = head;
return temp;
}
void Print(struct node* head)
{
struct node* tmp1 = head;
printf("List is:");
while (tmp1 != NULL) {
printf(" %d", tmp1->data);
tmp1 = tmp1->next;
}
printf("\n");
}
struct node* dupe(struct node* head, struct node* head2)
{
if (head == NULL)
return NULL;
struct node* temp = head;
struct node* prev = NULL;
struct node* cuurent = (struct node*)malloc(sizeof(struct node));
cuurent->data = temp->data;
if (head2 == NULL) {
cuurent->next = head2;
head2 = cuurent;
}
while (temp != NULL) {
temp = temp->next;
cuurent = (struct node*)malloc(sizeof(struct node));
cuurent->data = temp->data;
cuurent->next = prev;
prev = cuurent;
}
return head2;
}
int main(void)
{
head = NULL;
head2 = NULL;
head = Insert(head, 4);
head = Insert(head, 2);
head = Insert(head, 3);
head = Insert(head, 5);
head2 = dupe(head, head2);
Print(head);
Print(head2);
}
【问题讨论】:
-
您移动
temp = temp->next;并且不再检查temp在cuurent->data = temp->data;之前是否为空指针 - 您的逻辑在这里有缺陷 -
没有检查您的代码,但可能是未初始化或 NULL 指针。如果您包含回溯,这会很有帮助,您甚至可以自己看到答案。此外,值得一提的是您的编译器和平台,以获得潜在的提示。
-
我不明白
head2在这段代码中的作用是什么,无论是在main还是在你的dupe函数中。关于不递归复制链表,一个简单的前向链循环应该很简单,只需要大约 8 行函数代码。 -
所以用递归来做这件事对你来说不是问题吗?
-
我建议使用更多不同的标识符。编译器可能对全局变量和多个同名参数没有问题。但我不相信自己有那种狡猾的细节。
标签: c pointers linked-list