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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:42:05+00:00 2026-06-11T22:42:05+00:00

I am trying to create a bubble sort on a doubly linked linked list

  • 0

I am trying to create a bubble sort on a doubly linked linked list in Java but am getting Null Pointer Exception errors. I believe it to have an issue with when I call the getPrevious method on the head which of course has a value of null. However, I cannot think how to do the bubble sort without accessing the getPrevious method for the other nodes.

I can implement an if statement to check if its the head or tail of the list first, but I feel like there is a smarter way to do this.

I also have been unable to run a successful build of this, so am not even sure the code will work. If you have a different idea of how to implement this please let me know.

Any suggestions are welcome!

 public static void bubbleSort(DoubleLinkedList list) //static method used to sort the linked list using bubble sort
  {
      int i = 0;
      int j = 0;
      Node currentNode = list.head;
      Node previousNode = currentNode;
      Node tempNext =  currentNode;
      Node tempPrevious = currentNode;


      for(i=0; i<list.getSize(); i++)
      {
          for(j=0; j<list.getSize()-1; i++)
          {
              if(currentNode.getData() > currentNode.getNext().getData())
              {
                  tempNext = currentNode.getNext().getNext();
                  tempPrevious = currentNode.getPrevious();
                  currentNode.getPrevious().setNext(currentNode.getNext());
                  currentNode.getNext().setNext(currentNode);
                  currentNode.setPrevious(currentNode.getNext());
                  currentNode.setNext(tempNext);

              }

              currentNode = currentNode.getNext();

          }
      }



  }
  • 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-11T22:42:06+00:00Added an answer on June 11, 2026 at 10:42 pm

    So you have a double linked list. I assume each element contains some information… say an integer. It must also contain two pointers: one to the previous element and one to the next element.

    Assuming this is true, notice that you don’t have to modify the pointers because they already point from one element to another. all you have to do is sort the values of the list elements so that the first item in the list has the lowest value, the second has the second lowest value and so on.

    You can do it like this:

    public static void bubbleSort(DoubleLinkedList list) //static method used to sort the linked list using bubble sort {
          int i = 0;
          Node currentNode = list.head;
          Node auxNode;
          int foundChange = 1;
          while(foundChange) {
            foundChange = 0;
            for(i=0; i<list.getSize()-1; i++) {
              if (currentNode.getData() > currentNode.getNext().getData()) {
                auxNode.setData(currentNode.getData());
                currentNode.setData(currentNode.getNext.getData());
                currentNode.getNext.setData(auxNode.getData());
                foundChange = 1;
              }
              currentNode = currentNode.getNext();
            }
    
    }
    

    If you haven’t defined the setData method yet, then do so. It must be similar to getData, but it will set the data of an object to the value it gets as a parameter instead of returning the value of the data in that object.

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

Sidebar

Related Questions

I have been trying to implement Bubble Sort using simple integer array in java.
What I am trying to accomplish: Create a dynamic bubble that expands on height
Trying to create a black line in my view to separate text blocks but
Trying to create a new Dedicated Cache Role in Windows Azure but get the
Trying to create a background-image slideshow and am getting this error... This is the
I'm trying to create a simple function, but at runtime firebug says the function
I'm trying create a RCP Application with Eclipse, but I can't get past the
I have been trying to create a chat bubble for about a month now...
i'm trying to create my own jquery plugin but am having problems bubbling the
I'm trying to have a qTip2 bubble created but not displayed at load time.

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.