7-26 Windows訊息佇列(25 分)(堆排序)

來源:互聯網
上載者:User

標籤:lin   進程   win   -o   mes   scanf   改變   解題思路   card   

7-26 Windows訊息佇列(25 分)

訊息佇列是Windows系統的基礎。對於每個進程,系統維護一個訊息佇列。如果在進程中有特定事件發生,如點擊滑鼠、文字改變等,系統將把這個訊息加到隊列當中。同時,如果隊列不是空的,這一進程迴圈地從隊列中按照優先順序擷取訊息。請注意優先順序值低意味著優先順序高。請編輯程式類比訊息佇列,將訊息加到隊列中以及從隊列中擷取訊息。

輸入格式:

輸入首先給出正整數N(≤10?5??),隨後N行,每行給出一個指令——GETPUT,分別表示從隊列中取出訊息或將訊息添加到隊列中。如果指令是PUT,後面就有一個訊息名稱、以及一個正整數表示訊息的優先順序,此數越小表示優先順序越高。訊息名稱是長度不超過10個字元且不含空格的字串;題目保證隊列中訊息的優先順序無重複,且輸入至少有一個GET

輸出格式:

對於每個GET指令,在一行中輸出訊息佇列中優先順序最高的訊息的名稱和參數。如果訊息佇列中沒有訊息,輸出EMPTY QUEUE!。對於PUT指令則沒有輸出。

輸入範例:
9PUT msg1 5PUT msg2 4GETPUT msg3 2PUT msg4 4GETGETGETGET
輸出範例:
msg2msg3msg4msg1EMPTY QUEUE!


解題思路:本題主要是建立一個小頂堆,難一點的是輸出後的重建,需要類比一下過程
 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4  5 typedef struct Node *node; 6 struct Node 7 { 8     char mes[11]; 9     int priority;10 };11 12 struct13 {14     node heap[100005];15     int num;16 } Heap;17 18 void Put();19 void Get();20 21 int main()22 {23     int n;24     scanf("%d",&n);25     Heap.heap[0] = (node)malloc( sizeof(struct Node));26     Heap.heap[0]->priority = -1;27     Heap.num = 0;28 29     while( n--)30     {31         char op[4];32         getchar();33         scanf("%s",op);34         switch( op[0])35         {36         case ‘P‘ :37             Put();38             break;39         case ‘G‘ :40             Get();41             break;42         default :43             break;44         }45     }46 47     return 0;48 }49 50 51 void Put()52 {53     //讀入資料,建立一個小頂堆54     int i;55     node temp = ( node ) malloc( sizeof( struct Node));56     scanf("%s %d",temp->mes,&temp->priority);57     for( i=++Heap.num; Heap.heap[i/2]->priority > temp->priority; i=i/2)58     {59         Heap.heap[i] = Heap.heap[i/2];60     }61     Heap.heap[i] = temp;62 }63 64 void Get()65 {66     //輸出資料,重建頂堆67     int i;68 69     if( Heap.num<1)70     {71         printf("EMPTY QUEUE!\n");72         return ;73     }74     printf("%s\n",Heap.heap[1]->mes);75     for( i=1; i*2<Heap.num; )76     {77         if( i*2+1<Heap.num && Heap.heap[i*2+1]->priority<Heap.heap[i*2]->priority)78         {79             //如果有兩個根節點,並且右結點優先數小於左結點優先數80             if( Heap.heap[i*2+1]->priority<Heap.heap[Heap.num]->priority)81             {82                 Heap.heap[i] = Heap.heap[i*2+1];83                 i=i*2+1;84             }85             else break;86         }87         else88         {89             if(Heap.heap[i*2]->priority < Heap.heap[Heap.num]->priority)90             {91                 Heap.heap[i] = Heap.heap[i*2];92                 i *= 2;93             }94             else break;95         }96     }97     Heap.heap[i] = Heap.heap[Heap.num--]; //將最後的一個元素補在空缺98 }

 

 

7-26 Windows訊息佇列(25 分)(堆排序)

相關文章

聯繫我們

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