C++ Tutorials

C++ Tutorials

Swap() in C++

The function std::swap()

void num_exchange(int m, int n){
int temp;
temp = m;
m = n;
n = temp;
}
swap(m, n)

Hi, today we’ll be talking about how to swap nodes in a linked list without swapping the data The problem at our hand is that we are given a linked list and two keys in it and we are required to swap nodes for those two given keys. The nodes should be swapped by changing links.

First, let’s revise what a linked list is. Just like arrays, a linked list is a linear data structure Unlike arrays, linked lists elements are not stored at contiguous locations, they are linked using pointers Let’s take an example, we take this input, assuming all keys in the linked list are unique.

We take nodes to be 12 and 20 I.e. these two nodes are to be swapped The output for this would be 10, 15, 20, 13, 12, 14 hence seeing that 12 and 20 have been swapped This may look like a very simple problem, but it can be seen to be interesting when we see the following cases.

First, x and y may or may not be adjacent. Where x and y we know are the keys Either x or y may be a head node Either x or y may be the last node And the last case that we can see is if x and/or y are not present in the linked list We obviously have nothing to do if both x and y are the same. So the general idea is to first search the given linked list for the keys x and y.

If either x and y are not present, then return Now while searching for x and y, we keep track of the previous and current pointers In doing so, first change the next of the previous pointer and then change the next of the current pointer Here is a part of the code snippet of the swap function that one would implement to solve this problem.

Here currx and curry are the current pointers for x and y and prevx and prevy are the previous pointers for x and y. The last three lines are simply for swapping the next pointers.

How to use C++ swap function

Replace swap(&c[0],&d[0]) with swap(c[0], d[0])
&c[0] and &d[0] are rvalues (temporary objects of type int*)

How is std::swap implemented

template<typename T> void swap(T& t1, T& t2) {
    T temp = std::move(t1); // or T temp(std::move(t1));
    t1 = std::move(t2);
    t2 = std::move(temp);
}

swap() function in C++

void swap(int variable_name1, int variable_name2);

swap function c++ array

void swap (array& arr) noexcept(noexcept(swap(declval<value_type&>(),declval<value_type&>())));

swap function c++ array

C++ Array Library - swap() Function
A quick solution is to use std::swap().

swapping of two numbers in c++ using functions

  • 1. Call by Value
  • 2. Call by Reference
//Call by Value
#include<bits/stdc++.h>
#include<iostream>
using namespace std;

void swap(int ,int );
/*Swapping of Two Numbers in C++ Using Functions Call by Value*/
int main()
{
   //Program by- Pratap Das
    int a,b;
    cout<<"Enter the Two Numbers to Swap in C++: ";
    cin>>a>>b;
    cout<<"\nAfter Swapping of Two Numbers:";
    swap(a,b);
    
    return 0;
}
void swap(int x,int y)
{
 int z;
/*Extra veriable for storing the value of first or second variable*/ 
 
 z=x;
/*Copying the first variable value to the tempriory variable*/
 x=y;
/*Copying the second variable value to the first variable*/
 y=z;
/*Copying the tempriory variable value to the second variable*/ 
 cout<<" "<<x<<"   "<<y;
 
}

swap with reference c++

//Call by Reference
#include<bits/stdc++.h>
#include<iostream>
using namespace std;

void swap(int *x ,int *y );
/*Swapping of Two Numbers in C++ Using Functions Call by Reference*/
int main()
{
   //Program by- Pratap Das
    int a,b;
    cout<<"Enter Two Numbers To Swap: ";
    cin>>a>>b;
    
    swap(&a,&b);
    
    cout<<"\nAfter Swapping Two Numbers: ";
    cout<<a<<" "<<b<<" \n";
    
    return 0;
}
void swap(int *x,int *y)
{
 int z;
 z=*x;
/*Copying the first variable Address to the tempriory variable*/
 *x=*y;
/*Copying the second variable Address to the first variable*/
 *y=z;
/*Copying the tempriory variable Address to the second variable*/ 
}

swap characters in string c++

#include <string>
#include <algorithm>

std::string s = "03/02";
std::swap(s[1], s[4]);

letter swap c++

void swapuplo(char *s)
{
    size_t i;

    for (i = 0; s[i]; ++i)
        if (isupper(s[i]))
            s[i] = tolower(s[i]);
        else if (islower(s[i]))
            s[i] = toupper(s[i]);
}

This is the calling routine

int main()
{
    char buf[] = "HeLLo WORLD";
    swapuplo(buf);
    printf("%s\n", buf);

    getchar();
    return 0;
}

Output:

hellO world

std::swap pointers

You can use std::swap directly. Or code your swap function like the following:

void swap(int *r, int *s)
{
   int temp = *r;
   *r = *s;
   *s = temp;
   return;
} 

c++ program to swap two numbers using class

#include<iostream>
using namespace std;

class swap
{
int a,b;
public:
void getdata();
void swapv();
void display();
};

void swap::getdata()
{
cout<<“Enter two numbers:”;
cin>>a>>b;
}

void swap::swapv()
{
a=a+b;
b=a-b;
a=a-b;
}

void swap::display()
{
cout<<“a=”<<a<<“tb=”<<b;
}

int main()
{

swap s;

s.getdata();
cout<<“\nBefore swap: \n”;
s.display();

s.swapv();
cout<<“nnAfter swap:n”;
s.display();

return 0;
}