O (nlgk) problem priority queue implementation file C Language

Source: Internet
Author: User

/* Priorityqueue. C -- Minimum heap implementation file */<br/> # include "priorityqueue. H "</P> <p>/* local function declaration */</P> <p> static bool percolateup_p (prioritynode * const heap, const int position, const int size); <br/> static bool percolatedown_p (prioritynode * const heap, const int position, const int size ); </P> <p>/* interface function definition */</P> <p> bool initialize_p (priorityqueue * const ppq, const int size) <br/>{< br/> If (size <= 0) <br/> return False; <br/> * ppq = (struct priorityqueue *) malloc (sizeof (struct priorityqueue); <br/> If (null = * ppq) <br/> return false; <br/> (* ppq)-> heap = (prioritynode *) malloc (sizeof (prioritynode) * (size + 1 )); <br/> If (null = (* ppq)-> heap) <br/>{< br/> free (* ppq); <br/> return false; <br/>}< br/> (* ppq)-> current = 0; <br/> (* ppq)-> size = size; </P> <p> return true; <br/>}</P> <p> bool isem Pty_p (const priorityqueue * const ppq) <br/>{< br/> If (0 = (* ppq)-> current) <br/> return true; <br/> else <br/> return false; <br/>}</P> <p> bool isfull_p (const priorityqueue * const ppq) <br/> {<br/> If (* ppq)-> current = (* ppq)-> size) <br/> return true; <br/> else <br/> return false; <br/>}</P> <p> bool insert_p (const priorityqueue * const ppq, const prioritynode PN) <br/>{< br/> If (true = Isfull_p (ppq) <br/> return false; <br/> (* ppq)-> heap [++ (* ppq)-> current] = pN; <br/> percolateup_p (* ppq)-> heap, (* ppq)-> current, (* ppq)-> current ); </P> <p> return true; <br/>}</P> <p> prioritynode deletemin_p (const priorityqueue * const ppq) <br/>{< br/> prioritynode record; </P> <p> If (isempty_p (ppq) <br/> return NULL; <br/> record = (* ppq)-> heap [1]; <br/> (* ppq)-> heap [1] = (* ppq)-> Heap [(* ppq)-> current --]; <br/> percolatedown_p (* ppq)-> heap, 1, (* ppq)-> current ); </P> <p> return record; <br/>}</P> <p> bool increasekey_p (const priorityqueue * const ppq, const int position, const item delta) <br/>{< br/> If (position <1 | position> (* ppq)-> current | Delta <= 0) <br/> return false; <br/> (* ppq)-> heap [position]-> item + = delta; <br/> percolatedown_p (* ppq)-> heap, Position, (* ppq)-> current); </P> <p> return true; <br/>}</P> <p> bool decreasekey_p (const priorityqueue * const ppq, const int position, const item delta) <br/>{< br/> If (position <1 | position> (* ppq)-> current | Delta <= 0) <br/> return false; <br/> (* ppq)-> heap [position]-> item-= delta; <br/> percolateup_p (* ppq)-> heap, position, (* ppq)-> current); </P> <p> return true; <br/>}</P> <p> Void release_p (const priorityqueue * const ppq) <br/>{< br/> free (* ppq)-> heap); <br/> free (* ppq ); <br/>}</P> <p>/* local function definition */</P> <p> static bool percolateup_p (prioritynode * const heap, const int position, const int size) <br/>{< br/> prioritynode temp; <br/> int I; </P> <p> If (position <1 | position> size) <br/> return false; <br/> temp = heap [position]; <br/> for (I = position; parent (I)> = 1 & Heap [Parent (I)]-> item> temp-> item; I = parent (I )) <br/> heap [I] = heap [Parent (I)]; <br/> heap [I] = temp; </P> <p> return true; <br/>}</P> <p> static bool percolatedown_p (prioritynode * const heap, const int position, const int size) <br/> {<br/> prioritynode temp; <br/> int I, child; </P> <p> If (position <1 | position> size) <br/> return false; <br/> temp = heap [position]; <br/> for (I = Position; left (I) <= size; I = Child) <br/>{< br/> child = left (I); <br/> If (child! = Size & heap [child]-> item> heap [Child + 1]-> item) <br/> child = right (I ); <br/> If (temp-> item> heap [child]-> item) <br/> heap [I] = heap [child]; <br/> else <br/> break; <br/>}< br/> heap [I] = temp; </P> <p> return true; <br/>}

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.