Data structure and algorithm series (4) Priority queue

Source: Internet
Author: User
Tags min

Objective

We often encounter problems with stacks and queues in our lives, such as placing plates, fetching plates (similar stacks), and getting out of the collection, queuing (similar to queues) of first-in, first-out sets. Both of these situations are. NET already have related class library stack and queue, here no longer discuss, interested friends can Baidu this information. The main discussion here is the next priority queue, which is the extension on the queue basis.

1. Priority queue

As we all know, queues are an advanced first-out data structure. The effect of this behavior is to remove the first entry in the structure first. However, for many applications, a data structure that can remove the highest priority data item is needed, even if the data item is not "first entered" in the structure. A special case for this type of application queue is the priority queue.

Many applications use priority queues in their operations. A good example is the process processing within the computer operating system. Some processes may have higher precedence than other processes, such as a typical low priority for the printer process. Processes (or tasks) are usually numbered according to priority, and a process with a Priority (priority) of 0 has a higher priority than the Priority 20 task.

The data items stored in the precedence queue are typically constructed as key-value pairs, where the keyword refers to the priority, and the value is used to identify the data item. For example, you can define an operating system process in the following manner:

struct Process

{

int priority;

String name;

}

We cannot use an unmodified queue object for priority queues. The Dequeue method removes only the first item of data in the queue when it is invoked. However, you can derive your own priority queue class from the queue class, overwriting the Dequeue method to achieve your own needs.

We call this class Pqueue. All queue methods can be used as usual, overwriting the Dequeue method to remove the highest priority data items. In order not to remove data items from the front end of the queue, you first need to write the data entries for the queue to an array. The entire array is then traversed to find the data item with the highest priority. Finally, depending on the tagged data item, the queue can be rebuilt without regard to the tagged data item.

The code for the specific class is as follows:

public struct Pqitem

{

public int Priority;

public string Name;

}

public class Pqueue:queue

{

Public Pqueue ()

{

}

public override Object Dequeue ()

{

object[] items;

int min;

Items = this. ToArray ();

Min = ((Pqitem) items[0]). Priority;

for (int x = 1; x <= items. GetUpperBound (0); X + +)

{

if (((Pqitem) items[x]). Priority < min)

{

Min = ((Pqitem) items[x]). Priority;

}

}

This. Clear ();

int tmp;

for (TMP = 0; tmp <= items. GetUpperBound (0); tmp++)

{

if ((pqitem) items [tmp]). Priority = Min &&

((pqitem) items [tmp]). Name!= "")

This. Enqueue (Items[tmp]);

}

Return base. Dequeue ();

}

}

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.