template < class T> T Priority_Queue <T > :: Top ( ) { if ( front != NULL ) return front->info; return 0; } template < class T> void Priority_Queue <T> :: Pop () { PQNode * currentNode ; if (front == NULL) cout << "Priority Queue is Empty\n" ; else { currentNode = front; front = front->next; delete ( currentNode ); sz - -; } } 5 Example 1: Priority Queue using Linked List 3
template < class T> void Priority_Queue <T> :: Show () { for ( PQNode * t = front ; t != NULL ; t = t-> next ) cout << t->info << " " ; cout << endl ; } template < class T> Priority_Queue <T> :: ~ Priority_Queue ( ){ PQNode * currentNode ; while ( front != NULL ){ currentNode = front -> next ; delete front ; front = currentNode ; } } 6 Example 1: Priority Queue using Linked List 4
Used in certain implementation of Dijkstra ’s Shortest Path algorithm Anytime you need to dynamically fetch the “next best” or “next worst” element. Used in Huffman coding algorithm. Used in Best First Search (BFS) algorithm. Used in Minimum Spanning Tree in graphs. Applications of Priority Queue