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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:28:05+00:00 2026-06-16T08:28:05+00:00

I have a problem with my code, I have made a singly linked list

  • 0

I have a problem with my code, I have made a singly linked list class in which you can add, remove, modify, merge etc… however, I am attempting a simple bubble sort and have come across problems in which the list is not correctly sorted. here are some things to note:

  • it is a custom implementation of a linked list
  • the nodes of the singly linked list contain 2 things: a CustomerFile object with all the data for a customer and a ‘next’ node pointer to the next item in the list
  • the list is sorted in ascending order (A-Z) by the surname stored in the customer file of each node
  • the add record function inserts the nodes at the correct position in the list so that the list does not need to be sorted initially – however if the surname is changed, as part of the program, the list needs to be sorted again
  • I would rather not create a new list and re-use this insert record on that list to create a new list as this is memory intensive and my task is to be as efficient as possible
  • the very structure of the linked list cannot be changed – it is decided and I am too far to change to something such as an array
  • the list has a head node, it has next items but does not have a tail node. It has an appointed NULL next pointer to indicate the end pf the list

the code

public static void sortList()
{
    if (isEmpty() == true)
    {
        System.out.println("Cannot sort - the list is empty");
    }
    else if (getHead().getNext() == null)
    {
        System.out.println("List sorted");
    }
    else
    {
        Node current = getHead().getNext();
        CustomerFile tempDat;
        boolean swapDone = true;
        while (swapDone)
        {
            current = getHead().getNext();
            swapDone = false;
            while (current != null)
            {
                if (current.getNext() != null &&
                    current.getData().getSurname().compareTo(
                        current.getNext().getData().getSurname()) >0)
                {
                    tempDat = current.getData();
                    current.setData(current.getNext().getData());
                    current.getNext().setData(tempDat);
                    swapDone = true;
                }
                current = current.getNext();
            }
        }
        if (getHead().getData().getSurname().compareTo(
            getHead().getNext().getData().getSurname()) >0)
        {
            current = getHead().getNext();
            getHead().setNext(current.getNext());
            setHead(current);
        }
    }
}

I would appreciate the feedback

  • 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-16T08:28:06+00:00Added an answer on June 16, 2026 at 8:28 am

    Your code is an odd mixture, mostly swapping data but treating the head specially trying to swap it with the next by switching the links.

    As noted in another answer, this last swap is not correctly implemented, but really there’s no reason to treat the head specially if you’re doing things by swapping the data.

    All you have to do is start each traversal of the list at the head in the outer loop.

    This yields a working solution:

    public void sortList()
    {
        if (isEmpty())
        {
            System.out.println("An empty list is already sorted");
        }
        else if (getHead().getNext() == null)
        {
            System.out.println("A one-element list is already sorted");
        }
        else
        {
            Node current = getHead();
            boolean swapDone = true;
            while (swapDone)
            {
                swapDone = false;
                while (current != null)
                {
                    if (current.getNext() != null && current.getData().getSurname().compareTo(current.getNext().getData().getSurname()) >0)
                    {
                        CustomerFile tempDat = current.getData();
                        current.setData(current.getNext().getData());
                        current.getNext().setData(tempDat);
                        swapDone = true;
                    }
                    current = current.getNext();
                }
                current = getHead();
            }
        }
    }
    

    I’ve made this non-static because as noted in comments it wasn’t clear to me how yours could work as a static. You may be able to make it static in your context.

    I also changed a couple other minor things. It’s never necessary to check a condition by code like

    if (isEmpty() == true)
    

    In Java, this is entirely equivalent to

    if (isEmpty())
    

    And tempDat doesn’t need to be declared outside of where it’s used.

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

Sidebar

Related Questions

I have a problem, and I made this test code to show you my
I have a code problem which stems from the fact that I am using
I have a problem with my code. How can I handle an error from
I have problem with javascript code. I am using google maps and I am
I have problem with this code: file = tempfile.TemporaryFile(mode='wrb') file.write(base64.b64decode(data)) file.flush() os.fsync(file) # file.seek(0)
Hi i have problem with this code, i found it on the internet and
i am a beginner and i have a problem : this code doesnt compile
I have a problem in this code when I enter a string, instead of
I have a problem with this code: (The error is below the code) public
i have a problem with my code. foreach (DataRow dr in dt_pattern.Rows) { part

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.