Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 切换链表中的两个节点_C++_List_Object_Linked List - Fatal编程技术网

C++ 切换链表中的两个节点

C++ 切换链表中的两个节点,c++,list,object,linked-list,C++,List,Object,Linked List,我对编程相当陌生,我有这个任务,目标是使用这些函数打印0123456789然后9876543210 bool List::SortSwap() { bool doswap = true; ListElement *i =this-> head; ListElement *prev =this-> head; ListElement *b = i->next; while (doswap= true) { for (i;

我对编程相当陌生,我有这个任务,目标是使用这些函数打印0123456789然后9876543210

bool List::SortSwap()
{
    bool doswap = true;
    ListElement *i =this-> head;
    ListElement *prev =this-> head;
    ListElement *b = i->next;

    while (doswap= true) {
        for (i; i != NULL; i = i->next) {
            if (i != this-> head) {
                prev->next = i;
            }
            if (i->value < b->value) {
                swap(prev, i, b);
                doswap = true;
            }
        }

        doswap= false;
    }

    return doswap;
} 


void List::swap(ListElement * prev, ListElement * a, ListElement * b)
{
    prev->next = b;
    b->next = a;
}
bool列表::SortSwap()
{
bool doswap=true;
ListElement*i=此->头部;
ListElement*prev=此->头部;
ListElement*b=i->next;
while(doswap=true){
对于(i;i!=NULL;i=i->next){
如果(i!=此->头部){
上一个->下一个=i;
}
如果(i->valuevalue){
互换(上一、i、b);
doswap=true;
}
}
doswap=false;
}
返回doswap;
} 
作废列表::交换(ListElement*prev、ListElement*a、ListElement*b)
{
上一步->下一步=b;
b->next=a;
}

因此,如果doswap为真,它应该遍历我的链接列表,如果它的值小于下面的值,则交换元素,当我运行程序时,不会发生任何情况,我的列表不会打印到屏幕上,因此您尝试的是反转列表 我做过类似的事情

void LinkList::ReverseList()
{
    int Length=0;
    current=Head;
    while (current!=NULL)
    {

        current=current->link;
        Length++;

    }

    cout<<"Length is:"<<Length<<endl;
    Prev=current=Head;
    Temp=Head;
    Temp=Temp->link;
    for (int i= 0; i<Length-1; i++)
    {
        if (i==0)
        {
            Prev=Temp;
            current->link=NULL;
            Temp=Temp->link;
            Prev->link=current;
            current=Prev;

        }
        else
        {
            Prev=Temp;
            if (Temp->link!=NULL)
           {
              Temp=Temp->link;
           }

               Prev->link=current;
             current=Prev;

        }


    }
    Head=current;
}
void链接列表::ReverseList()
{
整数长度=0;
电流=水头;
while(当前!=NULL)
{
当前=当前->链接;
长度++;
}
coutlink=当前;
当前=上一个;
}
}
水头=电流;
}

如果
b
a
a
的继承人,则必须将
b
作为
prev
的继承人,将
a
作为
b
的继承人,以交换
a
b

void List::swap( ListElement * prev, ListElement * a, ListElement * b)
{
    a->next = b->next; // put successor of b in place of succesor of a
    b->next = a;       // put a in place of successor of b
    prev->next = b;    // put b in place of succesor of prev
}
要为列表交换连续的元素,必须处理列表的前两个元素。因此,清单的其余部分将一步一步地列出。如果到达最后一个列表元素的前驱者,则终止

bool List::SortSwap()
{
    if ( head == nullptr || head->next == nullptr )
        return false;

    bool anySwapped = false;
    // hadle head of list and its successor
    if ( head->value < head->next->value )
    {
        ListElement *temp = head;
        head = head->next;       // successor of head becomes new head
        temp->next = head->next; // successor of old head becomes successor of new head
        head->next = temp;       // new head becomes succesor of old head
        anySwapped = true;
    }

    // continue until predecessor of tail is found
    ListElement *prev = head;
    while ( prev->next != nullptr && prev->next->next != nullptr )
    {
        ListElement *a = prev->next;
        ListElement *b = a->next;
        if ( a->value < b->value )
        {
            swap( prev, a, b );
            anySwapped = true;
        }
        prev = prev->next; // step one forward
    }

    return anySwapped;
}
bool列表::SortSwap()
{
if(head==nullptr | | head->next==nullptr)
返回false;
bool anySwapped=false;
//哈德尔名单的头号人物及其继任者
如果(头->值<头->下一步->值)
{
ListElement*温度=水头;
head=head->next;//head的继任者成为新的head
temp->next=head->next;//旧head的继任者成为新head的继任者
head->next=temp;//新head取代旧head
anySwapped=true;
}
//继续,直到找到尾部的前一个
ListElement*prev=头部;
while(prev->next!=nullptr&&prev->next->next!=nullptr)
{
ListElement*a=prev->next;
ListElement*b=a->next;
如果(a->值值)
{
互换(上一、a、b);
anySwapped=true;
}
prev=prev->next;//前进一步
}
返回任何已交换的数据;
}

请注意,
(doswap=true)
是一项分配。循环
while(doswap=true)
是无限的。

假设在交换之前节点当前是这样排列的:

prev --> a --> b
prev --> b --> a
另外,假设这是节点在交换后的状态:

prev --> a --> b
prev --> b --> a
如果上述是掉期应该更改的,那么代码 在交换中,函数需要保存节点“b”的下一个节点 指针:


听起来您可能需要学习如何使用调试器逐步完成代码。有了一个好的调试器,您可以逐行执行您的程序,并查看它偏离预期的地方。这是一个必要的工具,如果你要做任何编程。进一步阅读:绘制相关节点及其当前连接。一个接一个地将连接移动到新状态。编写代码执行您必须进行的连接移动。在第一个while循环中,只需打印链接列表中的值。