Question

Write a routine to interchange the mth and nth elements of a singly-linked list. You may...

Write a routine to interchange the mth and nth elements of a singly-linked list. You may assume that the ranks m and n are passed in as parameters. Allow for all the ways that m and n can occur. You must rearrange the pointers, not simply swap the contents. Please use psuedocode

Homework Answers

Answer #1

The routine to swap the two nodes ranked m and n in the form of psuedocode is provided below, please comment if any doubts:

//The routine to swap m th and nth ranked elements
void swapmAndn(int m, int n)
{
   //Set the linked list head node
   Linked_Node **node_head

   //To find the m ranked node
   //set previous node  
   Linked_Node *node_prev = NULL;
  
   //set node to store m ranked node as head node
   Linked_Node *node_current_m = *node_head;
  
   //set the rank as rm=1
   rm =1

   //traverse to find the m ranked node
   while (node_current_m && rm<m)
   {    
       node_prev = node_current_m;
       node_current_m = node_current_m->next;
       rm++;
   }
  
   //To find the n ranked node
   //set previous node  
   Linked_Node *node_prev_n = NULL;
  
   //set node to store n ranked node as head node
   Linked_Node *node_current_n = *node_head;
  
   //set the rank as rn=1
   rn =1

   //traverse to find the n ranked node
   while (node_current_n && rn<n)
   {    
       node_prev_n= node_current_n;
       node_current_n = node_current_n->next;
       rn++;
   }
  
   //if m is not first ranked node
   if (node_prev != NULL)
       //set node_current_n as previous node
       node_prev->next = node_current_n;
  
   //otherwise Set the node_current_n
   else
       *node_head = node_current_n;
  
   // If n is not first ranked node
   if (node_prev_n!= NULL)
       node_prev_n->next = node_current_m;
   else
       *node_head = node_current_m;
  
   //now swap the next pointers also
   //to make the swap process complete  
   Linked_Node *temp = node_current_n->next;
   node_current_n->next = node_current_m->next;
   node_current_m->next = temp;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program to swap mth and nth elements of a linked list. Code needed in...
Write a program to swap mth and nth elements of a linked list. Code needed in java.
Write an iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse in...
Write an iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse in O(N) time. You can use as much extra space as you need. The original list pointers CAN NOT BE MODIFIED. State in big-O notation how much extra space is used by this algorithm. Write another iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse using O(1) extra space. The original list pointers CAN NOT BE MODIFIED. This algorithm can have...
Singly Linked List In Javascript - How to implement when given functions and tests to pass...
Singly Linked List In Javascript - How to implement when given functions and tests to pass ------------------------------------------------------------------------------------------------------------------------- So I am trying to implement a singly linked list that given the below function will pass the bottom four tests when ran in Node.js with the 'runTests()' command. Please help me generate the singly linked list that will work and pass the tests at the bottom. I will only use this as a reference so please add comments to explain what you...
Implement a singly linked list having all unique elements with the following operations.I 0 x –...
Implement a singly linked list having all unique elements with the following operations.I 0 x – Inserts element x at the end. I 1 y x – If the element y exists, then insert element x after the element y, else insert element y before the existing element x. Assuming either the element x or the element y exists. I 2 z y x – Inserts element x in the middle of the elements z and y. The element z...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that inserts 20 random integers from 0 to 100 in order in a linked list object. The program will create another linked list, but with 15 random integers from 0 – 100 in order. The program then will merge those two ordered linked list into a single ordered list. The function merge should receive references to each of the list objects to be merged and...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
Using c++ You may #include only: <iostream> <cmath> “functions.h” Write the function void readForceValuesFromStdIn(double* leftTeam, double*...
Using c++ You may #include only: <iostream> <cmath> “functions.h” Write the function void readForceValuesFromStdIn(double* leftTeam, double* rightTeam, unsigned const int noParticipants) that will read the list of forces exerted by each wizard alternating by the team into the correct array of doubles (see example in problem description) Define the function bool validForces(const double* forces, unsigned const int noParticipants) using the provided code. This function will be used to ensure that the forces exerted by each team’s wizard are non-negative The...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT