把兩個有序的迴圈線性組合成一個迴圈性性表演算法(c語言)

來源:互聯網
上載者:User

void merge(slnodetype *headA,slnodestype *headB)
{
  slnodetype *tempA ,*tempB,*temp;
  tempA=headA;
  tempB=headB;
  //迴圈每個A中元素,把比A中元素小的B中元素放在其前面
  while(tempA->next!=headA)
  {
   temp=tempA;

    //迴圈B中元素,把比A中元素小的B中元素放在A元素的前面
   while(tempA->next->data>tempB->next->data)
   {
     temp->next=tempB->next;
     tempB->next->next=tempA->next;
     temp=tempB-next;
   //下個B中元素;
     tempB=tempB->next;
     if(tempB->next==headB && tempA-next==headA)
     {
      //當到了A或B中的尾部時,組合完成,釋放B的頭結點
      free(headB)
      return;
     }
   }
    //下個A中元素
   tempA=temp->next;
  
  }
   //尋找B的尾部元素
  while(tempB->next!=headB)
  {
   tempB=tempB->next;
  }
  //把B尾部元素接在A的前面,釋放B的頭結點,組合完成
  tempB->next=headA;
  free(headB);

}

typedef struct slnode
{
Elemtype data;
struct slnode *next;

}slnodetype;

/*覺得一個好的演算法對提高程式的效能非常重要;初次接觸請多多指教*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.