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

时间:2014-03-13 15:21:28

标签: c++ c data-structures linked-list singly-linked-list

我试图在昨天找到链表中的最小两个数字,但仍然无法做到,所以决定在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 statments手动调试我发现min2 -> freq = INT_MAX;的初始化会产生问题(请参阅下面的完整代码),因为它无法打印&#34; check1&#34;只需打印&#34; check0&#34;

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

#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 ++ 吗?谢谢

1 个答案:

答案 0 :(得分:1)

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.

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