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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:25:11+00:00 2026-06-12T13:25:11+00:00

I have to write a program that will essentially sort nodes in a linked

  • 0

I have to write a program that will essentially sort nodes in a linked list. I have 5 functions that need to be written for this assignment and I am stuck on one of them. The function I am having trouble with is swapping two nodes. The header for the function is the following:

void swap (struct lnode** head, struct lnode* n1, struct lnode* n2)

I have gotten this to work properly as long as the two nodes are not next to each other. We have a list.h file provided to us and we are supposed to use two functions evictNode(struct lnode** head, struct lnode* node) and void insertNode (struct lnode** head, struct lnode* prevNode, struct lnode* nodeToBeInserted). These functions handle reassignment of the next and previous pointers as well. I am just not entirely sure on how to swap the nodes if they are next to each other. I have tried drawing it out but have not been able to wrap my mind around it.

Oh by the way, the way I handle the rest of the nodes is with the following code:

evictNode(head, n1);
evictNode(head, n2);
insertNode(head, n1prev, n2);
insertNode(head, n2prev, n1);

EDIT: Attempts

struct lnode* temp = n2;
insertNode(head,n1prev,temp)
evictNode(head, n2)

where struct lnode* n1prev = nodeGetPrev(n1)
there are two functions that return the previous/next pointers

  • 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-12T13:25:12+00:00Added an answer on June 12, 2026 at 1:25 pm

    I will do it as follow:

    struct lnode* tmp = nodeGetPrev(n1);
    /*
     * Remove n1 and insert it before n2
     * note to calling nodeGetPrev(n2) after removing n1
     */
    evictNode( head, n1 );
    insertNode( head, nodeGetPrev(n2), n1 );
    /* If n2 were before n1 and we insert n1 before n2, swap is done */
    if( tmp != n2 ) {
        evictNode( head, n2 );
        insertNode( head, tmp, n2 );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that looks like this. I need to consistently write something
I have a class assignment where I have to write a program that will
I have to write a program that will get lists in a list, and
for an assignment I have to write a program that will take in an
For class I have an assignment: Write a C++ program that will output the
I have a robot that I need to write an autonomous program for. The
I need to write a C program that will allow me to read/write files
I need to write a program that will take a existing local windows user,
I have to write an assembly program that will find the minimum of array
I have to write a simple program that will draw something in a Windows

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.