C 尝试遍历双链接列表时出现分段错误
这是我正在使用的一段代码:C 尝试遍历双链接列表时出现分段错误,c,linked-list,segmentation-fault,C,Linked List,Segmentation Fault,这是我正在使用的一段代码: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> int wordlen = 4; typedef struct Node { char* word; struct Node* next; struct Node* prev; }node; node* head; node * get
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int wordlen = 4;
typedef struct Node
{
char* word;
struct Node* next;
struct Node* prev;
}node;
node* head;
node * getWord(char* x)
{
node* newNode = malloc(sizeof(node));
newNode->word = x;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
void insertion(char* x)
{
node* temp = head;
node* newNode = getWord(x);
if (head == NULL)
{
head = newNode;
return;
}
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
void print()
{
node* temp = head;
while (temp != NULL)
{
printf("%s", temp->word);
temp = temp->next;
printf(" ");
}
printf("\n");
}
void sort()
{
char* a = malloc((wordlen + 1)*sizeof(char));
char* b = malloc((wordlen + 1)*sizeof(char));
node* temp = head;
while (temp != NULL)
{
a = temp->word;
temp = temp->next;
b = temp->word;
if (a[0] < b[0])
{
//temp->word = a;
//temp = temp->prev;
//temp->word = b;
}
}
}
int main(int argc, char *argv[])
{
insertion("asdk");
insertion("mapa");
insertion("klop");
sort();
print();
return 0;
}
#包括
#包括
#包括
#包括
int-wordlen=4;
定义表结点
{
字符*字;
结构节点*下一步;
结构节点*prev;
}节点;
节点*头;
node*getWord(char*x)
{
node*newNode=malloc(sizeof(node));
newNode->word=x;
newNode->next=NULL;
newNode->prev=NULL;
返回newNode;
}
空插入(字符*x)
{
节点*温度=头部;
node*newNode=getWord(x);
if(head==NULL)
{
头=新节点;
返回;
}
while(临时->下一步!=NULL)
温度=温度->下一步;
temp->next=newNode;
newNode->prev=temp;
}
作废打印()
{
节点*温度=头部;
while(temp!=NULL)
{
printf(“%s”,临时->单词);
温度=温度->下一步;
printf(“”);
}
printf(“\n”);
}
空排序()
{
char*a=malloc((wordlen+1)*sizeof(char));
char*b=malloc((wordlen+1)*sizeof(char));
节点*温度=头部;
while(temp!=NULL)
{
a=临时->文字;
温度=温度->下一步;
b=临时->文字;
if(a[0]word=a;
//温度=温度->上一个;
//temp->word=b;
}
}
}
int main(int argc,char*argv[])
{
插入(“asdk”);
插入(“mapa”);
插入(“klop”);
排序();
打印();
返回0;
}
分段错误出现在
sort()
函数中,尤其是变量b
中。我想到的是指针到达
NULL
时,因此当我试图返回(使用prev指针)时,列表的末尾会出现错误,因为我无法访问特定的内存块完全遍历链接列表后,我如何才能再次访问该列表的最后一个节点 您在
排序中有一个很大的内存泄漏:
char* a = malloc((wordlen + 1)*sizeof(char));
char* b = malloc((wordlen + 1)*sizeof(char));
...
a = temp->word; // This leaks a
...
b = temp->word; // This leaks b
不能在C中分配字符串,需要使用strcpy
复制它们
您还应该测试每个malloc
返回值是否为NULL
。请不要乘以sizeof(char)
,根据C标准的定义,它是1。如果要乘法,请使用sizeof(*a)
,无论a
指向哪种类型,这都是正确的。在sort
中存在大量内存泄漏:
char* a = malloc((wordlen + 1)*sizeof(char));
char* b = malloc((wordlen + 1)*sizeof(char));
...
a = temp->word; // This leaks a
...
b = temp->word; // This leaks b
不能在C中分配字符串,需要使用strcpy
复制它们
您还应该测试每个
malloc
返回值是否为NULL
。请不要乘以sizeof(char),根据C标准的定义,它是1。如果要乘法,请使用sizeof(*a)
,无论a
指向什么类型,这都是正确的。问题在于循环中,在取消引用之前,您没有检查temp->next
是否为空。当循环到达列表末尾时,您的temp->next
为空。将排序
和插入
中的循环条件更改为:
void sort()
{
char* a;
char* b;
node* temp = head;
while (temp && temp->next != NULL)
{
a = temp->word;
temp = temp->next;
b = temp->word;
if (a[0] < b[0])
{
temp->word = a;
temp = temp->prev;
temp->word = b;
}
}
}
void insertion(char* x)
{
node* temp = head;
node* newNode = getWord(x);
if (head == NULL)
{
head = newNode;
return;
}
while(temp && temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
void排序()
{
char*a;
char*b;
节点*温度=头部;
while(temp&&temp->next!=NULL)
{
a=临时->文字;
温度=温度->下一步;
b=临时->文字;
if(a[0]word=a;
温度=温度->上一个;
temp->word=b;
}
}
}
空插入(字符*x)
{
节点*温度=头部;
node*newNode=getWord(x);
if(head==NULL)
{
头=新节点;
返回;
}
while(temp&&temp->next!=NULL)
温度=温度->下一步;
temp->next=newNode;
newNode->prev=temp;
}
此外,您不需要为
a
和b
分配内存。您只需使用临时指针变量进行交换。问题在于您的循环在取消引用之前不检查temp->next
是否为空。当循环到达列表末尾时,您的temp->next
为空。将排序
和插入
中的循环条件更改为:
void sort()
{
char* a;
char* b;
node* temp = head;
while (temp && temp->next != NULL)
{
a = temp->word;
temp = temp->next;
b = temp->word;
if (a[0] < b[0])
{
temp->word = a;
temp = temp->prev;
temp->word = b;
}
}
}
void insertion(char* x)
{
node* temp = head;
node* newNode = getWord(x);
if (head == NULL)
{
head = newNode;
return;
}
while(temp && temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
void排序()
{
char*a;
char*b;
节点*温度=头部;
while(temp&&temp->next!=NULL)
{
a=临时->文字;
温度=温度->下一步;
b=临时->文字;
if(a[0]word=a;
温度=温度->上一个;
temp->word=b;
}
}
}
空插入(字符*x)
{
节点*温度=头部;
node*newNode=getWord(x);
if(head==NULL)
{
头=新节点;
返回;
}
while(temp&&temp->next!=NULL)
温度=温度->下一步;
temp->next=newNode;
newNode->prev=temp;
}
此外,您不需要为
a
和b
分配内存。您只需使用临时指针变量进行交换。嗯,您的程序有很多问题(特别是在排序函数中)
<强>第一< /强>,考虑到你是在while while循环中的最后一个元素。
while (temp != NULL)
{
a = temp->word;
temp = temp->next;
b = temp->word;
if (a[0] < b[0])
{
//temp->word = a;
//temp = temp->prev;
//temp->word = b;
}
}
}
解决方案:释放此内存
第三种,在C中不支持复制字符串的方式
a = temp->word;
b = temp->word;
解决方案:使用strcpy()
<强>第一< /强>,考虑到你是在while while循环中的最后一个元素。
while (temp != NULL)
{
a = temp->word;
temp = temp->next;
b = temp->word;
if (a[0] < b[0])
{
//temp->word = a;
//temp = temp->prev;
//temp->word = b;
}
}
}
解决方案:释放此内存
第三种,在C中不支持复制字符串的方式
a = temp->word;
b = temp->word;
解决方案:使用strcpy()您可以通过维护尾部指针和头部指针来访问最后一个节点。您甚至可以将它们保存在
节点
结构中,这样您只有一个与列表关联的变量。您可以通过维护尾部指针和头部指针来访问最后一个节点。你甚至可以把它们保存在一个节点
结构中,这样你就只有一个变量与列表相关联