简体   繁体   English

在C ++中使用气泡排序对链接列表进行排序

[英]Sorting linked list with bubble sort in c++

I have been trying to bubble sort a doubly linked list using swap function. 我一直在尝试使用交换功能对一个双向链表进行冒泡排序。 My question is does the swap function swap the pointer, and not just the data? 我的问题是交换函数是否交换指针,而不仅仅是数据? My code shows me it only swap the data but not the pointers. 我的代码向我展示了它仅交换数据,而不交换指针。 Is there any way to efficiently to swap the pointers on the linked list? 有什么方法可以有效地交换链接列表上的指针吗? Please show me the code as I am very inexperienced in coding and I do not understand other codes in other answer. 请告诉我代码,因为我在编码方面经验不足,并且我不理解其他答案中的其他代码。

void sortPoly(PolyNode* a)
{
  PolyNode* head =a;
  PolyNode* current = head;
  PolyNode* current_next = current->next;


  int len =Polylength(current);

  if(len ==1 || len ==0)
   {
      return;
   }

for(int i =0; i < len; i++)
{
    for (int j =0; j< len -i; j++)
    {
        int sum = current->expx + current->expy;
        cout << "sum=" << sum << endl;
        int next_sum = current_next->expx + current_next->expy;
         cout << "\t nextsum=" << next_sum << endl;

        if( sum < next_sum)
        {
            cout << "current=" << current->coef << "expx = " << current->expx << "expy=" << current->expy << endl;
            cout << "current_next=" << current_next->coef << "expx = " << current_next->expx << "expy=" << current_next->expy << endl;

            std:: swap(current, current_next);
            cout << endl;
            cout << "swapped" << endl;

            cout << "current=" << current->coef << "expx = " <<    current->expx << "expy=" << current->expy << endl;
            cout << "current_next=" << current_next->coef << "expx = " << current_next->expx << "expy=" << current_next->expy << endl;

           cout << "current=" << current->coef << "expx = " << current->expx << "expy=" << current->expy << endl;
           cout << "current_next=" << current_next->coef << "expx = " << current_next->expx << "expy=" << current_next->expy << endl;

        current = current->next;

        current_next = current->next->next;
        cout << "current=" << current->coef << "expx = " << current->expx << "expy=" << current->expy << endl;
        cout << "current_next=" << current_next->coef << "expx = " << current_next->expx << "expy=" << current_next->expy << endl;
    }
   }

 }

This is my struct: 这是我的结构:

    struct PolyNode
    {
      int coef;
      int expx;
      int expy;
      PolyNode* prev;
      PolyNode* next;
     };

Please find following my response on Mac terminal. 请在Mac终端上找到以下我的回复。 输入结果a = xy + 5-2x ^ 6 + 7x ^ 4y ^ 2 + 5x ^ 2-y ^ 2,b = y-x + 5

I think you probably have problems in swap function. 我认为您可能在交换功能上有问题。 I dont think std::swap can swap also prev and next reference. 我不认为std :: swap也可以交换上一个和下一个参考。 to be sure, you can implement your own swap. 当然,您可以实现自己的交换。 I would do this with this function. 我会用这个功能做到这一点。

void swap(PolyNode* node1, PolyNode* node2){
PolyNode* prev = node1->prev;
PolyNode* next = node2->next;
if (prev)
    prev->next = node2;
if (next)
    next->prev = node1;

node1->next = next;
node1->prev = node2;

node2->prev = prev;
node2->next = node1;
}

btw, you have to move iteration out of if. 顺便说一句,您必须将迭代移出if。

current = current->next;
current_next = current->next->next;

part has to be out of if if

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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