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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:20:22+00:00 2026-06-12T10:20:22+00:00

My nodes have an integer value. And I want my program to swap two

  • 0

My nodes have an integer value. And I want my program to swap two nodes by using such value:

void reemplazarNodosConValores(int a,int b) {
    node *antecedenteDelPrimero = antecedentePorValor(a);
    node *antecedenteDelSegundo = antecedentePorValor(b);
    node *primero = nodoPorValor(a);
    node *segundo = nodoPorValor(b);
    node *nextDelPrimero = siguienteNodo(primero);
    node *nextDelSegundo = siguienteNodo(segundo);


    antecedenteDelSegundo->next = primero;
    primero->next = nextDelSegundo;

    antecedenteDelPrimero->next = segundo;
    segundo->next = nextDelPrimero;
}

As you can see, I create six variables. The two nodes. The nodes before each one. And the nodes after each one.

Then, I do the swap.

However, swapping seem to generate an infinite loop when I try to print my nodes.

I based my swaps on this answer: https://stackoverflow.com/a/1536011/555690

EDIT: The other functions, as requested:

node *antecedentePorValor(int x) {
    node *resultado = NULL;
    for (int i = 0; i < counter; ++i) {
        resultado = siguienteNodo(resultado);
        if (siguienteNodo(resultado)) {
            if (siguienteNodo(resultado)->data == x) {
                break;
            }
        }
    }
    return resultado;
}

node *nodoPorValor(int x) {
    node *resultado = head;
    for (int i = 1; i < counter; ++i) {
        if (resultado->data == x) {
            break;
        }
        resultado = resultado->next;
    }
    return resultado;
}

node *siguienteNodo(node *nodo) {
    node *resultado;
    if (nodo) {
        resultado = nodo->next;
    }else{
        resultado = head;
    }
    return resultado;
}
  • 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-12T10:20:24+00:00Added an answer on June 12, 2026 at 10:20 am

    There are several cases you need to be careful with

    1. The node with value b immediately follows the node with value a or vice-versa
    2. The node with value a or b is the first node in the list
    3. There is more than one node with value a or value b

    Not possible to say whether you have a problem with case 2 or 3 without seeing the code for the functions antecedentePorValor and nodoPorValor. There is a bug with case #1.

    Edit: to show why there is a bug with #1, assume node(a) points to node(b), then

    void reemplazarNodosConValores(int a,int b) {
        node *antecedenteDelPrimero = antecedentePorValor(a); // node preceding a
        node *antecedenteDelSegundo = antecedentePorValor(b); // node preceding b (a)
        node *primero = nodoPorValor(a);                      // node that contains a
        node *segundo = nodoPorValor(b);                      // node that contains b
        node *nextDelPrimero = siguienteNodo(primero);        // node following a (b)
        node *nextDelSegundo = siguienteNodo(segundo);        // node following b
    
    
        antecedenteDelSegundo->next = primero; // a->next = a
        primero->next = nextDelSegundo;        // a->next = node following b
    
        antecedenteDelPrimero->next = segundo; // preva->next = b
        segundo->next = nextDelPrimero;        // b->next = b <<== b points to self!!
    }
    

    In the comments I used shorthand preva to mean antecedenteDelPrimero and a to mean primero and b to mean segundo.

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

Sidebar

Related Questions

All my TreeView nodes have a unique ID for their Node Depth. I want
I have this code for BinaryTree creation and traversal class Node { Integer data;
I have several nodes (undirected graph, bidirecional) ... Each two nodes have a distance...
The Scenario: We have multiple nodes distributed geographically on which we want to have
Hello I have a problem to retrieve a value from a method. I'm using
Using xslt/xpath, I need to be able to select the two elements which have
Sometimes I have to convert from an unsigned integer value to a float. For
I have a little problem. A node in my XML may contains and integer,
private void sumNode(TNode node) { int sum = 0; if (node == null) return;
I have a prefuse application that loads GraphML files where the nodes have a

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.