我需要帮助对此双链接列表(C )进行分类

I need help sorting this double linked list (C++)

本文关键字:分类 列表 链接 帮助      更新时间:2023-10-16

这就是我到目前为止所拥有的:

void sort(const E &t)
{
  DNode<E> *tmp = new DNode<E>(t,NULL,NULL);

    if(size==0)
    {
        cout << "List is empty" << endl;
    }
            else if(t<=head->element)
            {
                tmp->element=t;
                head->prev = tmp;
                tmp->next=head;
                head = tmp;
            }
                    else if(t>=tail->element)
                    {
                        tmp->element=t;
                        tail->next = tmp;
                        tmp->prev=tail;
                        tail = tmp;
                    }
                         curr=tmp;
                         insert(t);
                         size++;
} 

insert()只是我程序中的另一个函数:

 void insert(const E &t)
{
  DNode<E> *tmp = new DNode<E>(t,NULL,NULL);
  if (size == 0)
  { curr=head=tail=tmp; }
  else 
  {
    tmp->next=curr;
    tmp->prev=curr->prev;
    if (curr->prev) curr->prev->next=tmp;
    else { head=tmp; }
    curr->prev=tmp;
    curr=tmp;
  }
  size++;
}

它确实编译了,但没有给出正确的结果。我不确定我的错误是什么,我真的需要帮助。任何帮助将不胜感激。

这是我的主要程序中:

  one.sort(10);
  one.sort(20);
  one.sort(30);
  one.sort(40);
  one.sort(50);
  one.sort(60);
  one.print();
  one.moveToEnd();
  one.prev(); 
  one.prev();
  one.remove();
  one.remove();
  one.print();
  cout<<endl;

我应该得到这个:

head ==> 10-> 20-> 30-> 30-> 40-> 50-> 60&lt; ==尾巴head ==> 10-> 20-> 50-> 60&lt; ==尾巴

,但我得到了这个:头==> 10-> 20-> 20-> 30-> 30-> 30-> 40-> 40-> 40-> 50-> 50-> 50-> 60-> 60&lt;==尾巴head ==> 10-> 20-> 20-> 30-> 30-> 30-> 40-> 40-> 40-> 60-> 60&lt; ==尾巴

您看到的行为的原因是您缺少其他

这个:

curr=tmp;
insert(t);
size++;

是否在头部或尾巴上添加了一些东西,都将执行。由于您提供的每个条目都会添加到尾巴上,因此每次都会插入两次。如果您尚未将值添加到头部或尾部,则只能致电插入。

如果我正确理解,则应运行curr=tmp;size++;,无论如何,只有插入的呼叫才能在其他块中,我认为。

编辑:

应该看起来像这样:

if(size==0)
{
    cout << "List is empty" << endl;
    //Need to insert here as well, to add the first value to the list.
    insert(t);
}
        else if(t<=head->element)
        {
            tmp->element=t;
            head->prev = tmp;
            tmp->next=head;
            head = tmp;
        }
                else if(t>=tail->element)
                {
                    tmp->element=t;
                    tail->next = tmp;
                    tmp->prev=tail;
                    tail = tmp;
                }
                else 
                {
                     insert(t);
                }
                curr=tmp;
                size++;

我(Kinda)维护了您的空格使用情况,尽管我发现它有点不寻常。如果"否则"answers"其他"陈述,我通常将相关的''放在同一凹陷级别上,并进一步缩进嵌套块。我认为这是更标准的,但在这里也不是。

希望这会有所帮助:

    struct node {
       node* next;
       node* prev;
       Person p;
    };
    void sort(node* head) {
       node* n1;
       node* n2;
           for(n1 = head; n1->next != head; n1 = n1->next) {
                for( n2 = n1->next; n2 != head; n2 = n2->next) {
                       // swap data here if necessary
                 }
           }
      }

用于交换:

     X1 = currPtr->previoius;
     X2 = currPtr->next->next;
     currNext = currPtr->next;
     currNext->previous = currPtr->previous;
     currPtr->previous = currPtr->next;
     currPtr->next = currNext->next;
     currNext->next = currPtr;
     X1->next = currPtr->previous;
     X2->previous = currPtr;