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

  • Home
  • SEARCH
  • 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 8895453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:47:06+00:00 2026-06-14T23:47:06+00:00

Whats wrong with it? its supposed to sort it in ascending order. it doesn’t

  • 0

Whats wrong with it? its supposed to sort it in ascending order. it doesn’t cause the program to crash but when I’m pretty sure theres something wrong because when I the linked list to other functions it gets stuck in an infinite loop.

#include <stdio.h>
#include <stdlib.h>

struct dataTag {
   int key;
};

struct nodeTag {
   struct dataTag data;
   struct nodeTag *pNext;
};
typedef struct dataTag dataStructType;
typedef struct nodeTag nodeStructType;

nodeStructType *SortList(nodeStructType *pFirst)
{
    nodeStructType *swap,*ptr;
    if(pFirst == NULL)
        return NULL;
    else
    {
        swap = pFirst;
        ptr = pFirst -> pNext;
        while(ptr != NULL)
        {
            if(ptr -> data.key < swap -> data.key)
                swap = ptr;
            ptr = ptr -> pNext;
        }
        swap -> pNext = SortList(pFirst -> pNext);
        return swap;
    }
}
int main(void)
{
   nodeStructType *pFirst;
   /* Lets say codes exists here that makes a link list etc just to make the code short*/
   pFirst = SortList(pFirst);
   /*Free List*/
   return 0;
}
  • 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-06-14T23:47:07+00:00Added an answer on June 14, 2026 at 11:47 pm

    You cant just swap nodes to sort the list. Here is a link to a program that sorts singly linked lists.

    Basically the line
    swap = ptr;
    will not work as you think. You need to reset the links appropriately to ensure that your linked list remains linked correctly.

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

Sidebar

Related Questions

I can't trace whats inside holderMC, and its content. What's wrong here? instance.text=something holderMC.addChild(instance)
Why are my SQLSRV queries not executing? It doesn't echo anything like its supposed
Can anyone tell me whats wrong with this code. It's supposed to list the
Whats wrong with my buttons? here http://hiddenbrookstudio.com/index.swf , see how they rollover and dissappear
Whats wrong in below JSON Object definition I am trying to create a new
Whats wrong with this code? -(void) drawRect:(CGRect) rect { CGContextRef c = UIGraphicsGetCurrentContext(); if
Whats wrong with my query? ALTER TABLE test_posts ADD sticky boolean NOT NULL default
Whats wrong with this picture? Model: validates_acceptance_of :terms_of_service, :on => :create, :accept => true,
Please whats wrong with this code? Im using it to add some data to
It seems there's something wrong with this. I don't get any distinct errors, but

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.