本文整理汇总了C#中System.Collections.Generic.PriorityQueue.DequeueMax方法的典型用法代码示例。如果您正苦于以下问题:C# PriorityQueue.DequeueMax方法的具体用法?C# PriorityQueue.DequeueMax怎么用?C# PriorityQueue.DequeueMax使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Generic.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.DequeueMax方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PriorityQueueTest
static void PriorityQueueTest()
{
{
PriorityQueue<int> queueRemoveMin = new PriorityQueue<int>();
PriorityQueue<int> queueRemoveMax = new PriorityQueue<int>();
List<int> doubleCheckMin = new List<int>();
List<int> doubleCheckMax = new List<int>();
Random r = new Random();
// Generate the list of numbers to populate the queue and to check against
for (int i = 0; i < 20; i++)
{
int randInt = r.Next(-100, 100);
doubleCheckMin.Add(randInt);
}
for (int i = 0; i < doubleCheckMin.Count; i++)
{
int randInt = doubleCheckMin[i];
// heap.Add("" + i, i);
queueRemoveMin.Enqueue(randInt, randInt);
queueRemoveMax.Enqueue(randInt, randInt);
doubleCheckMax.Add(randInt);
}
doubleCheckMin.Sort(); // Default. Ascending
doubleCheckMax.Sort(delegate (int x, int y)
{
if (x == y) return 0;
if (x > y) return -1;
if (x < y) return 1;
return 0;
});
Console.WriteLine(" -- NOW REMOVE MIN --");
int checkCount = 0;
while (queueRemoveMin.Count > 0)
{
int min = queueRemoveMin.DequeueMin();
if (doubleCheckMin[checkCount] != min)
{
throw new Exception("WRONG!");
}
checkCount++;
Console.WriteLine(min);
}
Console.WriteLine(" -- NOW REMOVE MAX --");
checkCount = 0;
while (queueRemoveMax.Count > 0)
{
int max = queueRemoveMax.DequeueMax();
if (doubleCheckMax[checkCount] != max)
{
throw new Exception("WRONG!");
}
checkCount++;
Console.WriteLine(max);
}
}
// Now for some random fun. Randomly decide what operation we're performing.
// Sorted list is kept alongside for double-checking validity of heap results.
{
PriorityQueue<int> queue = new PriorityQueue<int>();
queue.DebugValidation = true;
List<int> list = new List<int>();
const int kMaxOperations = 2000;
int numOps = 0;
Random r = new Random();
for (numOps = 0; numOps < kMaxOperations; numOps++)
{
int randInt = r.Next(0, 4);
switch (randInt)
{
case 0:
case 1: // twice as likely to occur
{
// Add an item.
randInt = r.Next(-1000, 1000);
Console.WriteLine("Adding : " + randInt);
list.Add(randInt);
queue.Enqueue(randInt, randInt);
if (list.Count != queue.Count)
{
throw new Exception("Count mismatch!");
}
}
break;
case 2:
{
// Dequeue Min
list.Sort();
if (list.Count != queue.Count)
{
throw new Exception("Count mismatch! List= " + list.Count + ", queue = " + queue.Count);
}
if (list.Count == 0)
{
// well, can't do much here. early break
//.........这里部分代码省略.........