简体   繁体   中英

Swap the pointer in C++

Recently I am learning the Link-List in C++. Here is my code:

#include <iostream>

using namespace std;

class Node{
    Node* next;
    int num;

public:
    Node(int num){
        this->num = num;
    }

    void connect(Node* next){
        this->next = next;
    }

    Node* next_node(){
        return next;
    }

    void COUT(){
        cout<<this->num<<endl;
    }

};


void swap(Node* n1,Node* n2){
    static Node* n = n1;
    n1 = n2;
    n2 = n;

}

class List{

    Node* head;
    Node* last;

public:
    List(){
        head = 0;
        last = 0;
    }

    void insert(Node* n){
        if(head==0){
            head = n;
            last = n;
        }

        else{
           last->connect(n);
           last = n;
        }

    }

    void swap_head_last(){
        swap(head,last);
    }

};






int main()
{

    List* LL = new List();
    for(int i=0;i<10;i++){
        LL->insert(new Node(i));
    }

    LL->swap_head_last();



    return 0;
}

There were no errors until I tried to make "void swap_head_last()". What I want to do in this function is to make pointer "*head" point to the end of this list, and also make pointer "*last" point to the beginning of this list.

But when I tried to print these two pointer's value after calling this function, I found it is still point to the same object.

I've checked these two pages,

Swapping two pointers

C++ Swapping Pointers

But what I want is to change these two pointers direction rather than the object's value the pointer points to.

I know if I modify this function as below:

void swap_head_last(){
    static Node* n = head;
    head = last;
    last = n;
}

The result would be correct.

What's the problem in this function?

The root problem is that you can't easily swap nodes in a singly linked list. Actually the problem is that when moving away a node from it's position the successor of the predecessor has to be modified.

In addition your attempt on implementing swap does nothing, it just swaps two local variables (which will be forgotten as soon as the function has finished).

Instead you could use references to be able to swap:

void swap(Node*& n1, Node*& n2)
{
    Node* n = n1;
    n1 = n2;
    n2 = n;
}

But as pointed out this will still leave the problem that only the head and last will be swapped. The penultimate element will still have it's successor be the old ultimate element, you will have to update that pointer as well.

Try this:

void swap(Node** n1,Node** n2){
    Node* n = *n1;
    *n1 = *n2;
    *n2 = n;
}

Explination: If you would have to swap two integer you would need void swap(int*, int*).

Thus If you need to swap pointers, you would need void swap(int**, int**).

Is not very intuitive at first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM