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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:26:06+00:00 2026-05-27T00:26:06+00:00

public Node get(int i) { if (!isEmpty()) { int j = 0; Node element

  • 0
public Node get(int i) {  
    if (!isEmpty()) {  
        int j = 0;  
        Node element = head;  
        while (j++ < i) {  
            element = element.getNext();  
            if (element == null)  
                return null;  
        }  
        return element;  
    }  
    return null;  
}  



private void bubbleSort() {  
    for (int i = 0; i < size - 1; i++) {  
        boolean changed = false;  
        for (int j = 0; j < size - i - 1; j++) {  
            if (get(j + 1) != null) {  
                if (get(j).getValue() > get(j + 1).getValue()) {  
                    //System.out.println("Swapping: " + get(j).getValue() + " : " + get(j + 1).getValue());  
                    swap(get(j), get(j + 1));  
                    changed = true;  
                }  
            }  
        }  
        if (!changed)  
            return;  
    }  
}  

public void swap(Node first, Node mid) {  
    if (first == head)  
        head = mid;  
    if (mid == tail) {  
        tail = first;  
    }  
    first.setNext(mid.getNext());  
    mid.setPrev(first.getPrev());  
    first.setPrev(mid);  
    mid.setNext(first);  
}

I dont know what is missing…

List, value:
6
2
4
5
7

After sort:
2
6
7

Auxiliar outputs:
Swapping: 6 <> 2
Swapping: 6 <> 4
Swapping: 6 <> 5

Please, some on help-me, I don’t know sort a double linked list…..

public void swap(Node first, Node mid) {  
    if (first == head)  
        head = mid;  
    if (mid == tail) {  
        tail = first;  
    }  
    first.setNext(mid.getNext());  
    mid.setPrev(first.getPrev());  
    first.setPrev(mid);  
    mid.setNext(first);  
}

See the code following : swap function.

  • 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-27T00:26:07+00:00Added an answer on May 27, 2026 at 12:26 am

    Since this is appears to be a homework question I will not give you the full answer. I am fairly positive that the problem lies in your swap function. Consider drawing the nodes on paper and going through your swap function and seeing what happens to which nodes pointing where. Consider the previous and next nodes of the nodes you are swapping and that you have to update their previous/next as well! Unless of course your setNext and setPrev functions already do that.

    Feel free to comment if you need additional help.

    Edit 1:

    Also, as a side note, rather than using indices (i and j) to keep track of which node you’re using, consider just using the node itself. And then when you call i++ or j++, call node = node.getNext() instead. This way you aren’t adding an additional factor of n to your runtime, and you can eliminate your swap function altogether.

    Edit 2:

    What I mean by drawing it out on paper is write each of the values out on paper, in order, horizontally. Then draw a circle around each one and an arrow pointing to the next circle and the previous circle in each case. Draw the arrows in pencil! Then update the arrows accordingly as you step through your swap function. You don’t even have to move/erase the circles, just erase the arrows and see where they point next.

    Edit 3:

    Okay so you’ve got two circles that you want to swap. These circles both have arrows going outwards, and their neighbors both have arrows going inwards. That’s 4 arrows per circle (2 in, 2 out, unless you’re dealing with the head/tail). Since you are moving 2 circles, that means you have to change a total of 8 arrows (unless dealing with head/tail) – 4 per circle. You’re probably only changing one arrow per setPrev/setNext. Which implies that these should usually be called 8 times in your swap function. You’re only calling them 4 times.

    Edit 4:

    You are updating the prev/next of the swapped nodes, but not of their neighbors. You’re going to want to do something like first.getNext().setPrev(second), etc. to update the references/prev/next/arrows of the neighbors.

    Edit 5:

    Keep in mind null checks though (you can’t call setPrev or setNext on a null, so make sure when you set the neighbor’s prev/next that the neighbor actually exists (because prev doesn’t exist for head and next doesn’t exist for tail)).

    Edit 6:

    I guess if you say you aren’t doing homework… 😉

    void swap(Node first, Node second) {
        Node firstPrev = first.getPrev();
        Node firstNext = first.getNext();
        Node secondPrev = second.getPrev();
        Node secondNext = second.getNext();
    
        if (firstPrev) {
            firstPrev.setNext(second);
        }
        if (firstNext) {
            firstNext.setPrev(second);
        }
        if (secondPrev) {
            secondPrev.setNext(first);
        }
        if (secondNext) {
            secondNext.setPrev(first);
        }
    
        second.setPrev(firstPrev);
        second.setNext(firstNext);
        first.setPrev(secondPrev);
        first.setNext(secondNext);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this very simple C++ class: class Tree { public: Node *head; };
public class MyClass { public int Age; public int ID; } public void MyMethod()
Classes: public class Tree { public Node RootNode { get; set; } } public
I have a class as below: public class Node { public int NodeID {
I have a class class Node { public: int value; Node * next; Node();
I have this class: public class Source extends Node { protected DistributionSampler delay ;
public static void main(String[] args) { List<? extends Object> mylist = new ArrayList<Object>(); mylist.add(Java);
public class Test { public static void main(String[] args) { } } class Outer
public enum myEnum { VAL1(10), VAL2(20), VAL3(hai) { public Object getValue() { return this.strVal;
if i have the following node class class Node { public: Node() { next

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.