C++ I';我是C+的新手+;。I';在C+;中创建链表的代码中有错误+;使用SwapNodes函数。该函数不交换节点

C++ I';我是C+的新手+;。I';在C+;中创建链表的代码中有错误+;使用SwapNodes函数。该函数不交换节点,c++,linked-list,C++,Linked List,我正在尝试更改节点的数据以交换节点。感谢您的帮助。也许这是我对C++的一个基本错误。 我知道使用4指针交换节点的方法,这听起来很酷,但这有什么问题吗 #include <iostream> using namespace std; class Node { int data; Node *next; public: Node(int val) { data=val; next=NULL; }

我正在尝试更改节点的数据以交换节点。感谢您的帮助。也许这是我对C++的一个基本错误。 我知道使用4指针交换节点的方法,这听起来很酷,但这有什么问题吗

#include <iostream>

using namespace std;

class Node
{   

    int data;
    Node *next;
public:
    Node(int val)
    {
        data=val;
        next=NULL;
    }
    int Data(){return data;};
    void ChangeData(int newData){data=newData;};
    void setNext(Node *NEXT){next=NEXT;};
    Node *Next(){return next;};
};

class LinkedList
{
    Node *root;
public:
    LinkedList();
    void insert(int pos,int b);
    void deleteNode(int a);
    void outputList();
    void SwapNodes(int x, int y);
    void reverse();
};

LinkedList::LinkedList()
{
    root=NULL;
}

void LinkedList::insert(int pos,int b)
{
    Node *temp=root;
    Node *newnode=new Node(b);
    if(pos==0)
    {
        if(root==NULL)
        {
            root=newnode;
            return;
        }
        newnode->setNext(root);
        root=newnode;
        return;
    }
    for (int i = 1; i <pos ; ++i)
    {
        temp=temp->Next();
    }
    newnode->setNext(temp->Next());
    temp->setNext(newnode);
}

void LinkedList::deleteNode(int a)
{
    Node *temp=root;
    Node *agla=root->Next();
    if(temp->Data()==a)
    {
        root=root->Next();
        free(temp);
    }
    if(agla->Data()==a)
    {
        temp->setNext(agla->Next());
        free(agla);
        return;
    }
    while(agla!=NULL && agla->Data()!=a)
    {
        temp=agla;
        agla=agla->Next();
    }
    if(agla==NULL)
    {
        cout << "key not found";
    }
    temp->setNext(agla->Next());
    free(agla);

}

void LinkedList::SwapNodes(int x,int y)
{
    Node *temp1=root;
    Node *temp2=root;

    if(x==y)return;
    while(temp1!=NULL && (temp1)->Data()!=x)
    {
        (temp1)=(temp1)->Next();
    }
    temp1->ChangeData(y);

    while(temp2!=NULL && (temp2)->Data()!=y)
    {
        (temp2)=(temp2)->Next();
    }
    if(temp1==NULL || temp2==NULL)
    {
        return;
    }
    (temp2)->ChangeData(x);
}

void LinkedList::reverse()
{
    if(root == NULL) return;

    Node *prev = NULL, *current = NULL, *next = NULL;
    current = root;
    while(current != NULL){
        next = current->Next();
        current->setNext(prev);
        prev = current;
        current = next;
    }
    root = prev;
}


void LinkedList::outputList()
{
    Node *temp=root;
    if(temp==NULL)
    {
        cout << "empty";
        return;
    }

    while(temp!=NULL)
    {
        cout<< temp->Data()<< " ";
        temp=temp->Next();
    }
    cout << "\n";
}

int main(int argc, char const *argv[])
{
    LinkedList newList;
    newList.insert(0,4);
    newList.insert(1,5);
    newList.insert(2,7);
    newList.insert(3,9);
    newList.insert(4,12);
    newList.outputList();
    newList.insert(2,6);
    newList.outputList();
    newList.deleteNode(5);
    newList.outputList();
    newList.SwapNodes(7,9);
    newList.outputList();
    newList.reverse();
    newList.outputList();

    return 0;
}
#包括
使用名称空间std;
类节点
{   
int数据;
节点*下一步;
公众:
节点(int-val)
{
数据=val;
next=NULL;
}
int Data(){return Data;};
void ChangeData(int newData){data=newData;};
void setNext(Node*NEXT){NEXT=NEXT;};
Node*Next(){return Next;};
};
类链接列表
{
节点*根;
公众:
LinkedList();
无效插入(内部位置,内部b);
无效删除节点(INTA);
void outputList();
无效交换节点(整数x,整数y);
无效反向();
};
LinkedList::LinkedList()
{
root=NULL;
}
void LinkedList::insert(intpos,intb)
{
节点*temp=root;
Node*newnode=新节点(b);
如果(位置==0)
{
if(root==NULL)
{
根=新节点;
返回;
}
新建节点->设置下一步(根);
根=新节点;
返回;
}
对于(int i=1;i Next();
}
新建节点->设置下一步(临时->下一步());
temp->setNext(新节点);
}
void LinkedList::deleteNode(int a)
{
节点*temp=root;
Node*agla=root->Next();
如果(临时->数据()==a)
{
root=root->Next();
免费(临时);
}
如果(agla->Data()==a)
{
temp->setNext(agla->Next());
免费(agla);
返回;
}
while(agla!=NULL&&agla->Data()!=a)
{
温度=agla;
agla=agla->Next();
}
如果(agla==NULL)
{
cout setNext(agla->Next());
免费(agla);
}
无效链接列表::交换节点(整数x,整数y)
{
节点*temp1=根;
节点*temp2=根节点;
如果(x==y)返回;
而(temp1!=NULL&(temp1)->Data()!=x)
{
(temp1)=(temp1)->Next();
}
temp1->ChangeData(y);
while(temp2!=NULL&&(temp2)->Data()!=y)
{
(temp2)=(temp2)->Next();
}
if(temp1==NULL | | temp2==NULL)
{
返回;
}
(temp2)->ChangeData(x);
}
void LinkedList::reverse()
{
if(root==NULL)返回;
节点*prev=NULL,*current=NULL,*next=NULL;
电流=根;
while(当前!=NULL){
下一步=当前->下一步();
当前->设置下一步(上一步);
prev=当前值;
当前=下一个;
}
root=prev;
}
void LinkedList::outputList()
{
节点*temp=root;
if(temp==NULL)
{
不能In

你只是在撤销你已经做过的工作

while((temp1)->Data()!=x)
{
    (temp1)=(temp1)->Next();
}
temp1->ChangeData(y);
将查找
x
,然后将其设置为
y
。然后执行以下操作

while((temp2)->Data()!=y)
{
    (temp2)=(temp2)->Next();
}
(temp2)->ChangeData(x);
它发现您刚刚创建的
y
(只要没有以前的
y
)并将其设置回
x
。您需要做的是先找到要交换的节点,然后交换它们的数据。只需对

void LinkedList::SwapNodes(int x,int y)
{
    Node *temp1=root;
    Node *temp2=root;

    if(x==y)return;
    while((temp1)->Data()!=x)
    {
        (temp1)=(temp1)->Next();
    }
    while((temp2)->Data()!=y)
    {
        (temp2)=(temp2)->Next();
    }
    temp1->ChangeData(y);
    (temp2)->ChangeData(x);
}
如果
x
y
不存在,您还应该添加错误处理代码


不知道为什么到处都用括号,但是

(temp1)=(temp1)->Next();
可以写成

temp1 = temp1->Next();
循环,直到它找到一个值为
x
的链接。然后用
y
覆盖该值。好的。但是现在链接列表中有两个
y
s,所以可能性很大

     while((temp2)->Data()!=y)
    {
        (temp2)=(temp2)->Next();
    }
    (temp2)->ChangeData(x);
只需找到
节点
x
,然后将其更改回
x

    while((temp1)->Data()!=x)
    {
        (temp1)=(temp1)->Next();
    }
    while((temp2)->Data()!=y)
    {
        (temp2)=(temp2)->Next();
    }
    temp1->ChangeData(y);
    temp2->ChangeData(x);
将通过在更新其值之前同时查找
x
y
节点来防止出现这种情况,但存在其他问题。其中最重要的问题是当您在未找到
x
y
的情况下点击列表末尾时会发生什么

我会重新考虑链接逻辑,因为似乎没有任何方法可以判断您已经到达列表的末尾。典型的解决方案是将最后一个
节点的
next
设置为
NULL
nullptr
并使用类似的方法进行测试

while(temp1 != nullptr)

我在创建链表的代码中遇到错误,错误是?这到底出了什么问题?SwapNodes函数没有交换节点。
(temp1)=(temp1)->Next();
在temp1周围不需要括号。@KaustubhMundra那么,您是否尝试过使用调试器单步检查代码,以查看出现了什么问题?@KaustubhMundra没问题。很乐意提供帮助。@KaustubhMundra我强烈建议您在继续之前添加列表末尾检测。如果您没有这种遗漏,调试将更加容易挂在你的头上。我用括号是因为我试图通过将temp更改为Node**来做一些事情,这就是为什么,然后又将其还原:)我为误解道歉,@KaustubhMundra。我的意思是说更改源代码,而不是更改问题中的代码。更改问题中的代码以匹配是个坏主意(或在本例中部分匹配)答案,因为这会破坏导致答案的逻辑流。未来的读者将难以将旧答案与新代码匹配。
    while((temp1)->Data()!=x)
    {
        (temp1)=(temp1)->Next();
    }
    while((temp2)->Data()!=y)
    {
        (temp2)=(temp2)->Next();
    }
    temp1->ChangeData(y);
    temp2->ChangeData(x);
while(temp1 != nullptr)