0

I've written the following code for sorting a singly linked list using bubble sort. Sorting procedure should sort the nodes of the list on the basis of the data they contain. Sorting the nodes only exchanging the data among them won't be accepted. My code triggers a segmentation fault (core dumped). I think I'm not tracking the head pointer correctly. I'm sharing the full code for the reproduction of results.

#include<iostream>
using namespace std;   

typedef struct node
{
    int data;
    struct node *next;
} node;

node* create_node(node *head, int data)
{
    node *baby = (node*)malloc(sizeof(node));
    baby->data = data;
    baby->next = NULL;
    
    if(head==NULL)
        head = baby;
    else
    {
        node *curr = head;
        while(curr->next != NULL)
            curr = curr->next;
        curr->next = baby;  
    }
    return head;    
}

void sort_bubble(node *head)
{
    int count=0, i, j, temp;
    bool swapped;
    node *curr, *tempo, *p1, *p2;
    for(curr=head; curr!=NULL; curr=curr->next)
        count = count+1;
    
    for(i=0; i<=(count-2); i++)
    {
        curr = head;
        swapped = false;
        for(j=0; j<=(count-2-i); j++)
        {
            p1 = curr;
            p2 = p1->next;
            if(p1->data > p2->data)
            {
                tempo = p2->next;
                p2->next = p1;
                p1->next = tempo;
                curr = p2;
                swapped = true;
            }
            curr = curr->next;
        }
        if(swapped = false)
            break;
    }
}

void print_list(node *head)
{
    node *curr = head;
    while(curr)
    {
        cout<<curr->data<<" ";
        curr = curr->next;
    }
    cout<<"\n";
}

int main()
{
    int count = 6;
    node *head = NULL;
    
    head = create_node(head, 40);
    head = create_node(head, 10);
    head = create_node(head, 60);
    head = create_node(head, 50);
    head = create_node(head, 20);
    head = create_node(head, 30);
    print_list(head);
    
    sort_bubble(head);
    print_list(head);
    
    return 0;
}
4
  • Please add the tag for the language you are using Commented Oct 22, 2023 at 18:35
  • What use do you think curr = p2 is? Commented Oct 22, 2023 at 20:27
  • 1
    "My code triggers a segmentation fault (core dumped)." - Luckily for you, that's one of the easiest issues to catch and debug in your debugger. Either loading the core dump into the debugger or just running the program in the debugger should allow you to easily see the stack trace that lead to the fault and inspect relevant variables in the stack frame where it occurs, so you can deduce the cause of it. Btw; why not just use std::sort? Commented Oct 22, 2023 at 21:34
  • "I'm sharing the full code for the reproduction of results." -- Did you try to reduce the amount of code needed to reproduce the crash? For example, are you willing to claim that omitting head = create_node(head, 30); removes the crash? Did you identify which line crashes? (You should.) If it's crashing in sort_bubble then there's no need to call print_list() afterwards (in the code for this question) since execution never gets there. Commented Oct 22, 2023 at 23:30

1 Answer 1

0

Well, so far I can see this

if(swapped = false)

which makes the condition always false, and therefore the loop will never break. That makes the function run infinitely consuming CPU cycles which is probably what it throws the segmentation fault.

Sign up to request clarification or add additional context in comments.

1 Comment

No that's not the spoiler. Even after rectifying that the error persists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.