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 8718889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:39:30+00:00 2026-06-13T06:39:30+00:00

So what I am trying to do is to sort linked list. I’m stuck

  • 0

So what I am trying to do is to sort linked list. I’m stuck on breaking out of the loop where it seems that it never escapes. From examples I found all I had to do is to check if next value would be null, so what am I doing wrong?

Also, since I can’t figure out why this won’t brake out of loop, does this code seems like should do linked list sorting correctly?

    public void sortFirst() { 
        do
        {
            if (first.iData >= first.next.iData)
            {
                int iTempData = first.iData;
                double dTempData = first.dData;

                first.iData = first.next.iData;
                first.dData = first.next.dData;

                Link newLink = new Link(iTempData, dTempData);
                newLink.next = first;
                first = newLink;
            }
        }
        while (first.next != null);
    }   
  • 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-13T06:39:31+00:00Added an answer on June 13, 2026 at 6:39 am

    Well, right off the bat, you have a logic problem in your last 3 lines:

                Link newLink = new Link(iTempData, dTempData);
                newLink.next = first;
                first = newLink;
    

    Here, you set newLink.next = first. Therefore it isn’t null. Next you set first = newLink. So you can quickly see that first.next will never be null.

    From what I can tell in your loop, you are not actually shifting nodes around in your list,
    but only data within the nodes. In which case, you only need to move the data around:

                int iTempData = first.iData;
                double dTempData = first.dData;
    
                first.iData = first.next.iData;
                first.dData = first.next.dData;
    
                first.next.iData = iTempData;
                first.next.dData = dTempData;
    

    (and remove the last 3 lines which create the new node and insert it incorrectly in the list).
    Of course, that is a little ugly. The cool things with linked lists is that you can simply move the nodes around in your list, so you would do something like the following:

        current = head;
        prev = null;
        while( current != null && current.next != null ){
            next = current.next;
    
            if( current.iData > next.iData ) {
                // need to swap nodes
            if( prev == null ) {
                // special head case
                head = next;
                current.next = head.next;
                head.next = current;
                }
            else
            {
                prev.next = next;
                current.next = next.next;
                next.next = current;
            }
        }
    
        // increment pointer
        prev = current;
        current = current.next;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to figure out how to sort my doubly linked list. I get a
I've been trying to make some methods that will sort a linked list by
I'm trying to sort names into alphabetical order inside a linked list but am
Trying to sort an array in PHP that is being populated from a CSV.
I am trying to create a bubble sort on a doubly linked linked list
I am trying to sort a singly linked list which has been created and
I'm trying to create a linked list that is sorted by both name and
I am trying to sort a linked list. I am having problems with const.
hello i am trying to sort a linked list when i sort it it
I'm working on a university assignment. I am trying to write a linked-list sort

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.