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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:57:27+00:00 2026-06-17T00:57:27+00:00

I have a linked list with this values as examples: 4 5 3 2

  • 0

I have a linked list with this values as examples: 4 5 3 2 7, now I want to swap each node with previous one, like this:

4 5 3 2 7 // this beginning of list
5 4 3 2 7
5 3 4 2 7
5 3 2 4 7
5 3 2 7 4 // the list should now become like this

But unfortunately when I parse for the output I stuck in infinite loop:

#include <stdio.h>
#include <stdlib.h>

typedef struct _node {
    int p;
    struct _node *next;
} node;

main(int argc, char **argv)
{
    int i, n;
    node *nod = NULL;
    node *nod_tmp = NULL;
    node *nod2 = NULL;
    printf("Enter n: ");
    scanf("%d", &n);
    for(i = 0; i < n; ++i)
    {
        nod_tmp = (node *)malloc(sizeof(node));
        scanf("%d", &nod_tmp->p);
        nod_tmp->next = nod;
        nod = nod_tmp;
    }

    i = 0;
    while(i < n)
    {   
        nod_tmp = nod;
        nod = nod->next;
        nod->next = nod_tmp;
        ++i;
    }

    while(nod != NULL)
    {   

        printf("%d\n", nod->p);
        nod = nod->next;
    }
    return 0;
}  
  • 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-17T00:57:28+00:00Added an answer on June 17, 2026 at 12:57 am

    Your swap code is wrong. Should be something like this:

    i = 1;
    nod2 = nod;
    while(i < n)
    {   
        nod_tmp = nod2->next;
        nod2->next = nod_tmp->next;
        nod_tmp->next = nod2;
        ++i;
    }
    

    Or, since swapping every pair basically pushes the first element to the end you can do this:

    nod_tmp = nod;
    while (nod_tmp->next != NULL)
    {
        nod_tmp = nod_tmp->next;
    }
    // nod_tmp now points to the last element
    nod_tmp->next = nod;          // loop from the last element back to the first
    nod = nod->next;              // move the list pointer to the second element
    nod_tmp->next->next = NULL;   // break the loop at the new last element
    

    Also, you may want to look at your input code. As written it will build the list with the values in reverse order as entered, because it’s always adding the next value to the head of the list.

    Update

    To avoid potential seg_faults the first loop above can be rewritten without the counter like this:

    nod2 = nod;
    nod_tmp = nod2->next;
    while(nod_tmp != NULL)
    {   
        nod2->next = nod_tmp->next;
        nod_tmp->next = nod2;
        nod_tmp = nod2->next;
    }
    

    Update 2

    Here’s full code that does what you want. Rather than swap each pair of nodes it just pushes the first node to the end of the list. I’ve also fixed the input loop so the list gets built in the correct order.

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct _node {
        int p;
        struct _node *next;
    } node;
    
    main(int argc, char **argv)
    {
        int i, n;
        node *nod = NULL;
        node *nod_tmp = NULL;
        node *nod2 = NULL;
        printf("Enter n: ");
        scanf("%d", &n);
        for(i = 0; i < n; ++i)
        {
            nod_tmp = (node *)malloc(sizeof(node));
            scanf("%d", &nod_tmp->p);
            if (i == 0)
            {
                nod = nod2 = nod_tmp;
            }
            nod2->next = nod_tmp;
            nod2 = nod_tmp;
        }
    
        nod_tmp = nod;
        while (nod_tmp->next != NULL)
        {
            nod_tmp = nod_tmp->next;
        }
        // nod_tmp now points to the last element
        nod_tmp->next = nod;          // loop from the last element back to the first
        nod = nod->next;              // move the list pointer to the second element
        nod_tmp->next->next = NULL;   // break the loop at the new last element
    
        while(nod != NULL)
        {   
    
            printf("%d\n", nod->p);
            nod = nod->next;
        }
        return 0;
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Linked List and I want to implement a function: Random_Shuffle_List (struct
I have a singly-linked list containing 1 value in each element. struct listElement {
I have a singly linked list with node and list structs defined as: typedef
I have a vector of Linked list pointers. Each LinkedList has a head pointer
I have to implement a one linked list but it should put object in
I have a linked list of arrays (struct at bottom of post) Each array
I have a class like this - class Messages { ... LinkedList<String> inputs; LinkedList<String>
I have a linked list, which stores groups of settings for my application: typedef
Say you have a linked list structure in Java. It's made up of Nodes:
I have a generic linked-list that holds data of type void* I am trying

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.