Question

I am trying to find the minimum two numbers in a linked list since yesterday but still not able to do yet so decided to ask at stackoverflow.

My logic to do so is :

(1) First set min1->freq and min2->freq to INT_MAX they are both of node type pointer.

(2) Then i set the second smallest then the first smallest like this :

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;

Error:

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)

I debugged manually by printf statments i found that the initialization of min2 -> freq = INT_MAX; is creating problem (please see in my full code below) because it is not able to print "check1" just prints "check0"

If you want to have look to my full code then please find below :

#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);

}

Could anyone please help me in correcting in c/c++ ? Thanks

Was it helpful?

Solution

min2 is a pointer to which no memory has been allocated. Use new to allocate and delete to deallocate memory.

The way you write it:

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");

Explanation:

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.

The result is a attempt to write to random memory, which might result in a segfault.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top