我正在实现huffman压缩,我正在从包含节点的链接列表构建hufftree。在多次迭代之后,我在指向指定的指针上收到了段错误。根据我的经验和研究,我认为分段错误是由于程序中断以外的错误。任何帮助或建议将不胜感激。我是新来的堆栈溢出,从来没有问过一个问题,所以请让我知道如果你需要更多的信息来帮助我解决这个问题或其他任何问题。指针指向分割错误
struct LinkList{
int weight;
struct TreeNode * bottom;
struct LinkList * next;
}; typedef struct LinkList LinkList;
//This function takes in a link list where each node points to the next
//element in the link list and a huff tree node. It also contains weight
//which is equal to the weight of the hufftree that it points to.
TreeNode * huffTree(LinkList * head)
{
LinkList * temphead = head;
LinkList * ptr;
LinkList * ptr1;
int count = 127,i;
while(count>2)
{
temphead = head->next->next;
head->next->next = NULL;
head = mergeTree(head);
ptr = temphead;
ptr1 = temphead;// This is where I get the segmentation fault
//when the value of count is 14
while(head->weight>temphead->weight)
{
ptr1 = temphead;
temphead = temphead->next;
}
if(ptr1!=temphead)
{
head->next = temphead;
ptr1->next = head;
head = ptr;
}
else
{
head->next = temphead;
}
count--;
}
head = mergeTree(head);
TreeNode * root;
root = head->bottom;
head->bottom = NULL;
free(head);
return root;
}
LinkList * mergeTree(LinkList * head)
{
TreeNode * tree1 = head->bottom;
TreeNode * tree2 = head->next->bottom;
head->bottom = NULL;
head->next->bottom = NULL;
free(head->next);
free(head);
TreeNode * newnode = (TreeNode *) malloc(sizeof(TreeNode));
newnode->weight = tree1->weight + tree2->weight;
newnode->c = '~';
newnode->left = tree1;
newnode->right = tree2;
LinkList * ptr = (LinkList *) malloc(sizeof(TreeNode));
ptr->weight = newnode->weight;
ptr->bottom = newnode;
ptr->next = NULL;
return ptr;
}
这是C还是C++?它看起来像C. –
@PCLuddite - 是的它是c代码 –
@jakshay_desai然后你应该删除C++标记。 –