Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7054809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:40:21+00:00 2026-05-28T03:40:21+00:00

I am currently working on sorting a linked list in C as a homework

  • 0

I am currently working on sorting a linked list in C as a homework assignment. I’m not looking for a code snippet as an answer, as I understand the value of figuring it out myself. I am receiving a segfault using the function below and I would really appreciate if someone could at least tell me why. The best I could figure out is that it is failing when reaching the following line:

if ( head->value > head->next->value ) {

EDIT: Changed this line to if (head->next != NULL && head->value > head->next->value){ and am no longer receiving segfaults. However, my output head pointer is giving me the last node in the linked-list. HALP.

I’m not totally sure where to go from here, and even the slightest nudge in the right direction would be very appreciated.

struct node *sort_list(struct node *head) {
    bool swapped ;
    struct node * tmp , * orig ;
    orig = head ;

    if ( head == NULL || head->next == NULL ) return head ;
    else {
            do {
                    swapped = false ;
                    if ( head->next != NULL && head->value > head->next->value ) {
                            tmp = head ;
                            head = head->next ;
                            tmp->next = head->next ;
                            head->next = tmp ;

                            swapped = true ;
                    }
                    head = head->next ;
            } while ( swapped == true && head != NULL ) ;
    }
    return orig ;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:40:21+00:00Added an answer on May 28, 2026 at 3:40 am

    Once head becomes the last element in the linked list, you get your segfault.
    I don’t want to write the code since it’s homework, but add a conditional to check if head->next is null. If it is, you’ll want to set head back to the head of the list.

    Your bubble sort will need multiple passes through the linked list to sort it. If you initialize your linked list with values 5,4,3,2,1 and print head and print temp and head. You’ll probably see 5,4 5,3 5,2 5,1 segfault

    Also your sorting formula seems to be a bit off. If you have data such as 2,3,1. You’re code would see 2 and 3, swapped would become true and the function would return true.

    You may want to use a loop inside to loop so that every iteration of the outer loop will cause 1 pass through the linked list. If there are no swaps after a pass through the entire linked list, then the data is sorted.

    do{
        for 1 pass through linked list (this can be a for or while loop)
            swap if necessary; set swapped to true
    }while(swapped is true)
    

    Hope this helps.

    Edit

    After

    tmp = head;
    

    Add

    head = head->next  
    if(tmp == orig)  
        orig = head;  
    

    You need to maintain your head pointer.
    In the case of 5,4,3,6. It’ll sort it as follows
    4,5,3,6
    4,3,5,6
    3,4,5,6
    But your orig pointer was never updated, so your output will be truncated to 4,5,6.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a new code monkey in training, and I'm currently having issues working with
Currently working on re-writing several interfaces and looking at what options there are for
I'm working on an assignment for my Intro to Java course and I'm currently
I need to Sort Linked list using Selection sort. But I can not use
Currently working in the deployment of an OFBiz based ERP, we've come to the
Currently working on a VBScript to automate some of the dirty PST ingestion work
I currently working on an issue tracker for my company to help them keep
Am currently working on an application that requires users to submit posts and comments
I`m currently working out the design for simple graphic editor, who support trivial operations
Im currently working with an API which requires we send our collection details in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.