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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:46:40+00:00 2026-05-26T10:46:40+00:00

I implement selecting sort and I need to swap elements. I have doubly linked

  • 0

I implement selecting sort and I need to swap elements.
I have doubly linked list with previous and next links.
And link to the first and last element in List
I always need to swap some node b with first node toStartFrom

public void Swap(Node toStartFrom, Node b) {
        Boolean NextToEachOther = (toStartFrom.next == b);
        toStartFrom.next = b.next;
        b.previous = toStartFrom.previous;
        if (NextToEachOther) {
            toStartFrom.previous = b;
            b.next = toStartFrom;
        } else {
            toStartFrom.previous = b.previous;
            b.next = toStartFrom.next;
        }
    }

    public void display() {
        Node current = first;
        while (current != null) {
            ...printing...
            current = current.next;
        }
    }

But it doesn’t work.
No errors just doesn’t sort in right order.
And not diplays any elements after sort after toStartFrom node.

  • 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-26T10:46:40+00:00Added an answer on May 26, 2026 at 10:46 am

    You need to also update the nodes that are next to the 2 being swapped

    For instance, consider this list:

    first -> a -> b -> c

    If you wish to swap first and b then you must also update a’s and c’s next and prev references.

    Edit: This code should be placed before your code that does the swap and right after the Boolean declaration

    Edit2: Also, if you have refences to the head/tail of the list, you need to update those too. I don’t see that you referenced a head or tail anywhere in your code though.

    if(toStartFrom.prev != null)
    {
       toStartFrom.prev.next = b;
    }
    if(toStartFrom.next != b) // Equivalent to NextToEachOther
    {
       toStartFrom.next.prev = b;
    }
    if(b.next != null)
    {
       b.next.prev = toStartFrom;
    }
    if(b.prev != toStartFrom)  // Equivalent to NextToEachOther
    {
       b.prev.next = toStartFrom
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to Sort Linked list using Selection sort. But I can not use
In C++, I'm looking to implement an operator for selecting items in a list
I am trying to implement following scenario in the IPad environment. I have list
TLDR Version: I need to find a way to implement some sort of MessageCenter
I have the task to implement something similar to UIImagePickerController for displaying and selecting
I'm trying to implement a dynamic OPTION list in JavaScript. Depending on other selections
To implement data access code in our application we need some framework to wrap
I need to implement a Diff algorithm in VB.NET to find the changes between
I have tried to implement this jQuery active menu code: http://docs.jquery.com/Tutorials:Auto-Selecting_Navigation $(function(){ var path
So I'm trying to figure out how to implement a method of selecting lines

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.