Purchase Solution

C++: Delete largest value from the list (priority queue)

Not what you're looking for?

Ask Custom Question

Assume that a doubly linked list "header" stores the elements of a priority queue. Implement the function pop(), which deletes the element with the largest value from the list (priority queue).

template < typename T >
void pop(dnode< T > *header);

Here is my best Guess at the problem!

{
if ( header->next = = header)
return;

dnode< T > *prevNode = header->prev, *succNode = header->next;

prevNode->next = succNode;
succNode->prev = prevNode;

delete header;
}

Purchase this Solution

Solution Summary

This solution is more of a guidance. Solution first gives an implementation (function body) considering that the priority queue is maintained sorted in descending order. Then it considers that case that the priority queue is maintained sorted in ascending order and indicates what modification should be made to function body for descending case.

Solution Preview

If you are maintaining priority queue sorted in descending order then the largest value will be in the header node (assuming doubly linked list is non-empty). In that case logic for pop function will be like as you have already guessed in right direction.

{
dnode < T > *newHeader; // Useful, in case header is passed by reference and you want changes to be reflected back.

if (header == NULL) // This signifies empty ...

Purchase this Solution


Free BrainMass Quizzes
Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

Javscript Basics

Quiz on basics of javascript programming language.