Pointer values can be used for exclusive OR operation of exchanging the two variables

The pointer is not directly XOR operation, it is necessary to convert a pointer to an integer int or long, the Linux system can be long, because the pointer 4 bytes in the win system, occupy 6 bytes in the Linux system.

The following is an exclusive OR operation of the two pointers a pointer-exchange:

    #include <stdio.h> int main () 
    { int * A, * B; 
        unsigned Long AA, BB; 
        the printf ( " A: P% \ n- " , A); 
        the printf ( " B: P% \ n- " , B); 
        AA = (unsigned Long ) A;     // A pointer variable is converted into an integer 
        BB = (unsigned Long ) B;     // B pointer variable is converted into an integer   
        the printf ( " \ n- " ); 
        AA ^ = BB; 
        BB ^ = AA;     //
    
    
        
    
    

    At this time = AA BB 
        AA BB = ^;     // this time = BB AA 
    
        A = ( int *) AA;         // AA is converted into a pointer variable pointer type 
        B = ( int *) BB;         // BB is converted into a pointer variable pointer type 
    
        the printf ( " A: P% \ n- " , A); 
        the printf ( " B: P% \ n- " , B); 
       
        return  0 ; 
    }

Operating results in Linux 64 system is:

    a:0x7ffc70da4cf0
    b:0x4004e0
    
    a:0x4004e0
    b:0x7ffc70da4cf0

So we can see that the exchange value of the two is not necessarily only with temporary variables, the XOR operation is also an option.

The above code from: https://blog.csdn.net/weixin_39666151/article/details/90731384

Attached below the inverse operation to set the linear form using a single linked list implementation:

#include <the iostream> 
#include <bits / STDC ++ H.>
 #define int Long Long
 the using  namespace STD; 
typedef struct 
{ 
    int len;
     int * Next;
     // int listsize; // this is the order of the table, this question is not requirements 
} Node; 

// set the inverse function 
void inverse (* Node List) 
{ 
    for ( int I = 0 ; I <list-> len / 2 ; I ++) // first switching element and the last element, the second penultimate element and switching element, and so on, as long as half of the table can also be used to exchange a temporary variable value, doing so requires additional storage space, ^ denotes exclusive oR operation 
    { 
        List->next[i] = list->next[i]^list->next[list->len - i - 1];
        list->next[list->len - i - 1] = list->next[i]^list->next[list->len - i - 1];
        list->next[i] = list->next[i]^list->next[list->len - i - 1];
    }

}

void Print(node *list)
{
    for(int i = 0;i < list->len;i++)//遍历 
        cout << list->next[i] << " ";
    cout << endl;
}


signed main()//Main function, and destruction initialization sequence table directly in the main function 
{
     int I = 0 ; 
    node * List; // definition of a structure node pointer 
    List = (node *) the malloc ( the sizeof (node)); // dynamic memory pointer for the application 
    
    COUT << " Please enter the number of elements: " ; 
    CIN >> list-> len; // length of the input sequence table 
    list-> Next = ( int *) the malloc ( the sizeof ( int ) * list-> len); // is the sequence table dynamic application memory of fixed length, cast to type int, then re-set may be used during the inverse exclusive-oR operator, using the pointer itself is not an exclusive oR operation 
    
    COUT << "Please input elements: " ;
     for (I = 0 ; I <list-> len; I ++) // loop input element, the array pointer is stored 
        >> CIN list-> Next [I]; 
    
    COUT << " front facing Inverse table: " ; 
    the Print (List); // custom function output 
    inverse (List); // set reverse operation 
    COUT << " table after the counter is the inverse: " ; 
    the Print (List); // output is set after the inverse the 
    
    Free (list-> Next); // free space opened, do not forget pointer structure body 
    Free (List); 
    
    return  0 ; 
}

Attached below set inverse linear table, order table implemented

#include<iostream>
#define int long long
#include<map>
using namespace std;

signed main()
{
    int n;
    int vis[100];
    cin >> n;
    for(int i = 1;i <= n;i++)
        cin >> vis[i];
    int i = 1;
    int j = n;
    while(i < j)
    {
        int temp;
        temp = vis[i];
        vis[i] = vis[j];
        vis[j] = temp;
        i++;
        j--;
    }
    for(int i = 1;i <= n;i++)
        cout << vis[i] << " ";
    cout << endl;    
    return 0;
}

The operation of the above-mentioned single chain ponder the study and understanding of good

Guess you like

Origin www.cnblogs.com/biaobiao88/p/12042132.html
Recommended