gpt4 book ai didi

c++ - 在链表中使用 INT_MAX 查找两个最小值会产生段错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:48:40 25 4
gpt4 key购买 nike

从昨天开始,我一直试图在链表中找到最小的两个数字,但仍然无法做到,所以决定在 stackoverflow 上提问。

我这样做的逻辑是:

(1) 首先设置min1->freq和min2->freq为INT_MAX,它们都是节点类型指针。

(2) 然后我像这样设置第二小的然后是第一个最小的:

while (temp != NULL) 
{
printf("\ncheck2\n");
if ((temp) -> freq < min2 -> freq)
{
printf("check3\n");
min1 = min2;
min2 = temp;
}
else if ((temp) -> freq < min1 -> freq && (temp) -> freq != min2 -> freq)
{
printf("check4\n");
min1 = temp;
}
temp = temp -> next;
}
* lmin1 = min1;
* lmin2 = min2;

错误:

The error i am getting is this :
enter the size of node
4
start entering the number of elements until your size
0
-1
-5
8
Printing linked list
0-> -1-> -5-> 8->
check0
Segmentation fault (core dumped)

我通过 printf 语句手动调试,我发现 min2 -> freq = INT_MAX; 的初始化正在产生问题(请参阅下面的完整代码),因为它无法print "check1"只打印 "check0"

如果您想查看我的完整代码,请在下面找到:

#include <stdio.h> 
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <limits.h>

struct node
{
int freq;
struct node * next;
};
typedef struct node node;
node * tree;

//Problem creating area is below (code for finding minimum two elements)
void find_two_min(node * * List, node * * lmin1, node * * lmin2)
{

node * temp = * List;
node * min1;
min1 -> freq = INT_MAX;
node * min2;
printf("\ncheck0\n");
min2 -> freq = INT_MAX; //This initialisation of INT_MAX to min2->freq creates problem because printf() statment above it works well but don't work below it.
printf("check1\n");
while (temp != NULL)
{
printf("\ncheck2\n");

if ((temp) -> freq < min2 -> freq)
{
printf("check3\n");
min1 = min2;
min2 = temp;
}
else if ((temp) -> freq < min1 -> freq && (temp) -> freq != min2 -> freq)
{
printf("check4\n");
min1 = temp;
}
temp = temp -> next;
}
* lmin1 = min1;
* lmin2 = min2;
printf("check5\n");
}
//Problem creating area is above//
void main()
{
int size, data;
node * min1;
node * min2;
int count = 0; //this count flag is to check is it's first node or not inside the do-while loop.
tree = NULL;
printf("enter the size of node\n");
scanf("%d", & size);
printf("start entering the number of elements until your size\n");
node * prev;
do {
scanf("%d", & data);
if (count == 0)
{
node * temp;
temp = (node * ) malloc(sizeof(node));
temp -> freq = data;
temp -> next = NULL;
prev = temp;
tree = prev;
}
else
{
node * temp;
temp = (node * ) malloc(sizeof(node));
temp -> freq = data;
temp -> next = NULL;
prev -> next = temp;
prev = prev -> next;
}
size--;
++count;
}
while (size > 0);

printf("Printing linked list\n");
node * temp1;
temp1 = tree;
while (temp1 != NULL)
{
printf("%d-> ", temp1 -> freq);
temp1 = temp1 -> next;
}
node * temp5 = tree;
find_two_min( & temp5, & min1, & min2);
printf("\n The two minimum numbers are min1 :%d and min2 : %d\n", min1 -> freq, min2 -> freq);

}

有人可以帮我在 c/c++ 中更正吗?谢谢

最佳答案

min2 是一个没有分配内存的指针。使用 new 分配内存,使用 delete 释放内存。

你的写法:

node * min2;
printf("\ncheck0\n");
min2 -> freq = INT_MAX; //This initialisation of INT_MAX to min2->freq creates problem because printf() statment above it works well but don't work below it.
printf("check1\n");

解释:

let `min2` be a pointer to a memory area holding a node. The pointer is random.
initialize the member `freq` of the structure pointed to by min2.

结果是尝试写入随机内存,这可能会导致段错误。

关于c++ - 在链表中使用 INT_MAX 查找两个最小值会产生段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22383192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com