K個有序鏈表共N個結點在O(NlgK)時間合并為一個新的有序鏈表實現檔案C語言

來源:互聯網
上載者:User

先是在 main () 函數中構造輸入.之後調用主常式解決問題.可以看到,主常式是相當精妙的.這當然是需要前面資料結構的正確.

/*MergeQueue.c -- O (NlogK) 時間將K個有序隊列合并為一個新有序隊列*/<br />#include "Queue.h"<br />#include "PriorityQueue.h"</p><p>#define SIZE (5)</p><p>int main (void) ;<br />BOOL merge (const Queue * const array, const int number, Queue * const pNewQueue) ;</p><p>int main (void)<br />{<br />Queue array[SIZE] ;<br />Queue q1, q2, q3, q4, q5, newQueue ;<br />Item item ;</p><p>/*Artificial element.*/<br />Initialize_Q (&q1) ;<br />Initialize_Q (&q2) ;<br />Initialize_Q (&q3) ;<br />Initialize_Q (&q4) ;<br />Initialize_Q (&q5) ;</p><p>item = 1 ;<br />EnQueue_Q (&q1, item) ;<br />item = 2 ;<br />EnQueue_Q (&q1, item) ;<br />item = 2 ;<br />EnQueue_Q (&q1, item) ;<br />item = 4 ;<br />EnQueue_Q (&q1, item) ;<br />item = 20 ;<br />EnQueue_Q (&q1, item) ;</p><p>item = 3 ;<br />EnQueue_Q (&q2, item) ;<br />item = 7 ;<br />EnQueue_Q (&q2, item) ;<br />item = 8 ;<br />EnQueue_Q (&q2, item) ;<br />item = 8 ;<br />EnQueue_Q (&q2, item) ;<br />item = 9 ;<br />EnQueue_Q (&q2, item) ;<br />item = 16 ;<br />EnQueue_Q (&q2, item) ;</p><p>item = 45 ;<br />EnQueue_Q (&q3, item) ;<br />item = 56 ;<br />EnQueue_Q (&q3, item) ;<br />item = 77 ;<br />EnQueue_Q (&q3, item) ;</p><p>item = 14 ;<br />EnQueue_Q (&q4, item) ;<br />item = 67 ;<br />EnQueue_Q (&q4, item) ;<br />item = 78 ;<br />EnQueue_Q (&q4, item) ;<br />item = 89 ;<br />EnQueue_Q (&q4, item) ;<br />item = 90 ;<br />EnQueue_Q (&q4, item) ;<br />item = 92 ;<br />EnQueue_Q (&q4, item) ;</p><p>item = 11 ;<br />EnQueue_Q (&q5, item) ;<br />item = 22 ;<br />EnQueue_Q (&q5, item) ;<br />item = 33 ;<br />EnQueue_Q (&q5, item) ;<br />item = 34 ;<br />EnQueue_Q (&q5, item) ;<br />item = 35 ;<br />EnQueue_Q (&q5, item) ;<br />item = 37 ;<br />EnQueue_Q (&q5, item) ;<br />item = 45 ;<br />EnQueue_Q (&q5, item) ;<br />item = 56 ;<br />EnQueue_Q (&q5, item) ;<br />item = 67 ;<br />EnQueue_Q (&q5, item) ;</p><p>array[0] = q1 ;<br />array[1] = q2 ;<br />array[2] = q3 ;<br />array[3] = q4 ;<br />array[4] = q5 ;</p><p>/*Initialize new queue.*/<br />Initialize_Q (&newQueue) ;<br />/*It's so attractive!*/<br />merge (array, SIZE, &newQueue) ;</p><p>Release_Q (&q1) ;<br />Release_Q (&q2) ;<br />Release_Q (&q3) ;<br />Release_Q (&q4) ;<br />Release_Q (&q5) ;<br />Release_Q (&newQueue) ;</p><p>return 0 ;<br />}</p><p>/*O (NlogK)*/<br />BOOL merge (const Queue * const array, const int number, Queue * const pNewQueue)<br />{<br />PriorityQueue pq ;<br />PriorityNode temp ;<br />int i ;</p><p>if (number <= 0 || FALSE == Initialize_P (&pq, number))<br />return FALSE ;<br />/*Bulid heap.*/<br />for (i = 0; i < number; i++)<br />Insert_P (&pq, array[i] -> front) ;<br />/*Core loop.*/<br />while (FALSE == IsEmpty_P (&pq))<br />{<br />temp = DeleteMin_P (&pq) ;<br />EnQueue_Q (pNewQueue, temp -> item) ;<br />if (temp -> next != NULL)<br />Insert_P (&pq, temp -> next) ;<br />}<br />Release_P (&pq) ;</p><p>return TRUE ;<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.