Priority Queue C language implementation

Source: Internet
Author: User

The priority queue is implemented by the heap, and the implementation of the heap is mentioned earlier, and one important operation of the priority queue is:

1. Heap_max O (1)

2. Heap_extract_max O (LGN)

3. Heap_increase_key O (LGN)

4, Heap_insert O (LGN)

The following is the C language implementation

#define Min-100000int Heap_max (struct heap *h) {    return h->arr[1];} int Heap_extract_max (struct heap *h) {    int max = h->arr[1];    H->ARR[1] = h->arr[h->size];    h->size--;    Max_heapify (H, 1);    return Max;} void Heap_increase_key (struct heap *h, int pos, int key) {    if (H->arr[pos] < key) {        H->arr[pos] = key;
   
    while (pos > 1 && h->arr[pos] > H->ARR[POS/2]) {            swap (& (H->arr[pos]), & (H->arr[p OS/2]));            POS/= 2;}}}    void Heap_insert (struct heap * h, int key) {    //sets the element after the last element to infinity and then increases its key value    h->arr[++ (h->size)] = MIN;    Heap_increase_key (H, h->size, key);}
   













Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Priority Queue C language implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.