【问题标题】:Sorting a Linked List in ascending order and printing sorted List以升序对链接列表进行排序并打印已排序的列表
【发布时间】:2020-09-02 08:37:00
【问题描述】:

我正在尝试按升序对链接列表进行排序并停留在此处。其余代码工作正常(附加,前置函数)。我在这里尝试使用冒泡排序算法。

但是,输出显示分段错误。我在这里做错了什么?

void sortLinkedList(Node** head_ref)
{
    Node* slow_node =(*head_ref);
    Node* fast_node=NULL;
    Node* temp=NULL;
    while(slow_node->next!=NULL)
    {
        fast_node=slow_node->next;
        while(fast_node->next!=NULL)
        {
            if(fast_node->data>fast_node->next->data)
            {
                temp->data=fast_node->data;
                fast_node->data=fast_node->next->data;
                fast_node->next->data=temp->data;
            }   
            fast_node=fast_node->next;
        }
        slow_node=slow_node->next;
    }
}

void printList(Node** head_ref)
{
    Node* new_node=(*head_ref);

    while(new_node!=NULL)
    {
        cout<<new_node->data<<"-->";
        new_node=new_node->next;
    }
    cout<<"NULL";
    cout<<endl;
}



int main()
{
    Node* head=new Node();

    head=NULL;

    insertAtEnd(&head,2);
     printList(&head);
    insertAtEnd(&head,3);
     printList(&head);  
    insertAtEnd(&head,2);
     printList(&head);  
    insertAtEnd(&head,4);
     printList(&head);  
     insertAtEnd(&head,5);
     printList(&head);  

    cout<<"Sorted List"<<endl;
    sortLinkedList(&head);
    printList(&head);

}

输出

2-->NULL
2-->3-->NULL
2-->3-->2-->NULL
2-->3-->2-->4-->NULL
2-->3-->2-->4-->5-->NULL
Sorted List
Segmentation fault (Core dumped)

【问题讨论】:

  • 你试过用调试器运行它吗?
  • 节点定义在哪里?
  • ... 你在哪里分配 temp?
  • Node** 传递给打印函数是没有意义的。使用const Node*
  • 通常链表排序会重新指向节点,而不是交换数据。

标签: c++ linked-list c++14 bubble-sort


【解决方案1】:

你有

Node* temp=NULL;

然后你

temp->data=fast_node->data;

因为temp 是一个空指针,所以它会繁荣起来。

如果您要交换节点的数据,则不需要整个节点,只需 data 的任何类型之一即可:

 if(fast_node->data>fast_node->next->data)
 {
     whatever_data_is temp = fast_node->data;
     fast_node->data = fast_node->next->data;
     fast_node->next->data = temp;
 }   

但您的标准库中已经有一个交换功能,因此您可以简化:

 if (fast_node->data>fast_node->next->data)
 {
     std::swap(fast_node->data, fast_node->next->data);
 }   

【讨论】:

    【解决方案2】:

    冒泡排序中的问题,是交换操作。您使用为 NULL 的 temp 并尝试访问数据元素。这会触发分段错误。

    在最简单的情况下,您可以使用std::swap。你的冒泡排序看起来像

    void sortLinkedList(Node** head_ref)
    {
        Node* slow_node =(*head_ref);
        Node* fast_node=NULL;
        while(slow_node->next!=NULL)
        {
            fast_node=slow_node->next;
            while(fast_node->next!=NULL)
            {
                if(fast_node->data>fast_node->next->data)
                {
                    std::swap(fast_node->data, fast_node->next->data);
                }   
                fast_node=fast_node->next;
            }
            slow_node=slow_node->next;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-03
      • 2015-06-30
      • 2012-11-07
      • 2016-10-18
      • 2017-02-22
      • 2013-10-16
      • 2020-10-05
      相关资源
      最近更新 更多