本文整理汇总了C#中PriorityQueue.Dequeue方法的典型用法代码示例。如果您正苦于以下问题:C# PriorityQueue.Dequeue方法的具体用法?C# PriorityQueue.Dequeue怎么用?C# PriorityQueue.Dequeue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.Dequeue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestPriorityQueue
public void TestPriorityQueue()
{
int operationsCount = 100000;
Random rand = new Random(0);
PriorityQueue<double> queue = new PriorityQueue<double>();
for (int op = 0; op < operationsCount; ++op)
{
int opType = rand.Next(0, 2);
if (opType == 0) // Enqueue
{
double item = (100.0 - 1.0) * rand.NextDouble() + 1.0;
queue.Enqueue(item);
Assert.IsTrue(queue.IsConsistent(), "Test fails after enqueue operation # " + op);
}
else // Dequeue
{
if (queue.Count > 0)
{
double item = queue.Dequeue();
Assert.IsTrue(queue.IsConsistent(), "Test fails after dequeue operation # " + op);
}
}
}
}
示例2: Main
static void Main()
{
PriorityQueue<int> queue = new PriorityQueue<int>();
queue.Enqueue(10);
queue.Enqueue(3);
queue.Enqueue(14);
queue.Enqueue(12);
queue.Enqueue(5);
queue.Enqueue(2);
Console.WriteLine("Printing the queue");
foreach (var item in queue)
{
Console.WriteLine(item);
}
Console.WriteLine("Dequeue {0}", queue.Dequeue());
Console.WriteLine("Dequeue {0}", queue.Dequeue());
Console.WriteLine("Printing the queue again");
foreach (var item in queue)
{
Console.WriteLine(item);
}
}
示例3: Main
/* 1 Implement a class PriorityQueue<T> based
* on the data structure "binary heap".
* */
static void Main(string[] args)
{
var heap = new Heap<int>();
heap.Add(1);
heap.Add(2);
heap.Add(3);
Debug.Assert(heap.SameContents(new[] { 1, 2, 3 }));
Console.WriteLine(string.Join(",", heap));
Debug.Assert(heap.ChopHead() == 3);
Debug.Assert(heap.ChopHead() == 2);
Debug.Assert(heap.ChopHead() == 1);
Debug.Assert(heap.IsEmpty);
// higher string means lower priority
var pqueue = new PriorityQueue<string, string>((s1, s2) => -s1.CompareTo(s2));
pqueue.Enqueue("18:00", "Buy food");
pqueue.Enqueue("06:00", "Walk dog");
pqueue.Enqueue("21:00", "Do homework");
pqueue.Enqueue("09:00", "Go to work");
pqueue.Enqueue("21:00", "Drink beer");
Debug.Assert(pqueue.Count == 5);
Debug.Assert(pqueue.Dequeue() == "Walk dog");
Debug.Assert(pqueue.Dequeue() == "Go to work");
Debug.Assert(pqueue.Dequeue() == "Buy food");
Debug.Assert(new[] { "Do homework", "Drink beer" }.Contains(pqueue.Dequeue()));
Debug.Assert(new[] { "Do homework", "Drink beer" }.Contains(pqueue.Dequeue()));
}
示例4: CorrectPriorityTest1
public void CorrectPriorityTest1()
{
PriorityQueue<int> queue = new PriorityQueue<int>();
queue.Enqueue(-12);
queue.Enqueue(512);
queue.Enqueue(77);
queue.Enqueue(-1);
queue.Enqueue(82);
queue.Enqueue(92);
queue.Enqueue(-111);
queue.Enqueue(-151);
queue.Enqueue(512);
queue.Enqueue(55);
Assert.AreEqual(10, queue.Count);
Assert.AreEqual(512, queue.Dequeue());
Assert.AreEqual(512, queue.Dequeue());
Assert.AreEqual(92, queue.Dequeue());
Assert.AreEqual(82, queue.Dequeue());
Assert.AreEqual(77, queue.Dequeue());
Assert.AreEqual(55, queue.Dequeue());
Assert.AreEqual(-1, queue.Dequeue());
Assert.AreEqual(-12, queue.Dequeue());
Assert.AreEqual(-111, queue.Dequeue());
Assert.AreEqual(-151, queue.Dequeue());
}
示例5: DequeueTest
public void DequeueTest()
{
var testQueue = new PriorityQueue<int>();
testQueue.Enqueue(10, 0);
testQueue.Enqueue(5, 0);
testQueue.Enqueue(6, 1);
Assert.IsTrue(testQueue.Dequeue() == 6);
Assert.IsTrue(testQueue.Dequeue() == 10);
Assert.IsTrue(testQueue.Dequeue() == 5);
}
示例6: Main
public static void Main(string[] args)
{
var priorityQueue = new PriorityQueue<int>();
priorityQueue.Enqueue(3, 3);
priorityQueue.Enqueue(3, 3);
priorityQueue.Enqueue(4, 4);
priorityQueue.Dequeue();
priorityQueue.Dequeue();
Console.WriteLine(priorityQueue.Peek());
}
示例7: Main
public static void Main()
{
var minPriorityQueue = new PriorityQueue<int>();
minPriorityQueue.Enqueue(12);
minPriorityQueue.Enqueue(3);
minPriorityQueue.Enqueue(7);
Console.WriteLine(minPriorityQueue.Dequeue());
Console.WriteLine(minPriorityQueue.Dequeue());
Console.WriteLine(minPriorityQueue.Dequeue());
}
示例8: Simple
public void Simple()
{
var priorityQueue1 = new PriorityQueue<int, int>(PriorityQueueType.Minimum);
priorityQueue1.Enqueue(4);
Assert.AreEqual(priorityQueue1.Count, 1);
Assert.AreEqual(priorityQueue1.Dequeue(), 4);
priorityQueue1.Enqueue(5);
priorityQueue1.Enqueue(6, 2);
Assert.AreEqual(priorityQueue1.Dequeue(), 5);
Assert.AreEqual(priorityQueue1.Dequeue(), 6);
priorityQueue1.Enqueue(6, 2);
priorityQueue1.Enqueue(5);
Assert.AreEqual(priorityQueue1.Dequeue(), 5);
Assert.AreEqual(priorityQueue1.Dequeue(), 6);
var priorityQueue2 = new PriorityQueue<string, int>(PriorityQueueType.Minimum);
priorityQueue2.Enqueue("a", 1);
priorityQueue2.Enqueue("b", 2);
priorityQueue2.Enqueue("c", 3);
priorityQueue2.Enqueue("d", 4);
priorityQueue2.Enqueue("e", 5);
priorityQueue2.Enqueue("f", 6);
priorityQueue2.Enqueue("z", 6);
priorityQueue2.Enqueue("y", 5);
priorityQueue2.Enqueue("x", 4);
priorityQueue2.Enqueue("w", 3);
priorityQueue2.Enqueue("v", 2);
priorityQueue2.Enqueue("u", 1);
priorityQueue2.Enqueue("z", 1);
priorityQueue2.Enqueue("y", 2);
priorityQueue2.Enqueue("x", 3);
priorityQueue2.Enqueue("w", 4);
priorityQueue2.Enqueue("v", 5);
priorityQueue2.Enqueue("u", 6);
Assert.AreEqual(priorityQueue2.Count, 18);
priorityQueue2.Clear();
Assert.AreEqual(priorityQueue2.Count, 0);
}
示例9: Main
public static void Main(string[] args)
{
PriorityQueue<int> heap = new PriorityQueue<int>(2);
heap.Enqueue(1);
heap.Enqueue(2);
heap.Enqueue(3);
heap.Enqueue(4);
heap.Dequeue();
heap.Enqueue(3);
heap.Enqueue(5);
Console.WriteLine(heap.Dequeue());
heap.Print();
heap.Enqueue(2);
heap.Print();
}
示例10: Enqueue
public void Enqueue()
{
var priorityQueue = new PriorityQueue<string, int>(PriorityQueueType.Maximum) { DefaultPriority = 2 };
priorityQueue.Enqueue("test1");
priorityQueue.Enqueue("test2", 3);
Assert.AreEqual(priorityQueue.Dequeue(), "test2");
Assert.AreEqual(priorityQueue.Dequeue(), "test1");
priorityQueue.Enqueue("test1");
priorityQueue.Enqueue("test2", 1);
Assert.AreEqual(priorityQueue.Dequeue(), "test1");
Assert.AreEqual(priorityQueue.Dequeue(), "test2");
}
示例11: PriorityQueue_Dequeue_NonRepeatedValues_Success
public void PriorityQueue_Dequeue_NonRepeatedValues_Success()
{
// Arrange
var priorityQueue = new PriorityQueue<int>();
var values = new int[] { 10, 7, 6, 1, 2, 3, 5, 4, 9, 8 };
var expected = "1,2,3,4,5,6,7,8,9,10";
// Act
foreach (var value in values)
{
priorityQueue.Enqueue(value);
}
var result = "";
var index = 0;
var lastIndex = priorityQueue.Count() - 1;
while (!priorityQueue.IsEmpty())
{
var element = priorityQueue.Dequeue();
result += index++ == lastIndex ?
string.Format("{0}", element.ToString()) : string.Format("{0},", element.ToString());
}
// Assert
Assert.AreEqual(result, expected);
}
示例12: Main
/* Task 1:
* Implement a class PriorityQueue<T> based on the data structure "binary heap".
*/
static void Main(string[] args)
{
var prioQueue = new PriorityQueue<int>();
prioQueue.Add(25);
prioQueue.Add(15);
prioQueue.Add(105);
prioQueue.Add(5);
prioQueue.Add(35);
Console.WriteLine(prioQueue.Count());
Console.WriteLine(prioQueue.Dequeue());
Console.WriteLine(prioQueue.Dequeue());
Console.WriteLine(prioQueue.Dequeue());
Console.WriteLine(prioQueue.Dequeue());
}
示例13: DijkstraAlgorithm
public static void DijkstraAlgorithm(Graph graph, Node source)
{
foreach (var node in graph)
{
node.MinDistance = double.PositiveInfinity;
}
source.MinDistance = 0;
var pQueue = new PriorityQueue<Node>();
pQueue.Enqueue(source);
while (pQueue.Count != 0)
{
Node currentNode = pQueue.Dequeue();
foreach (var neighbour in graph[currentNode.Id].Neighbors)
{
double newDist = currentNode.MinDistance + neighbour.Distance;
if (newDist < neighbour.Node.MinDistance)
{
neighbour.Node.MinDistance = newDist;
pQueue.Enqueue(neighbour.Node);
}
}
}
}
示例14: DijkstraAlgorithm
private static Dictionary<char, string> DijkstraAlgorithm(Node<char> startingNode, HashSet<char> visitedNodes)
{
Dictionary<char, string> paths = new Dictionary<char, string>();
paths[startingNode.Symbol] = startingNode.Symbol.ToString();
PriorityQueue<Node<char>> queue = new PriorityQueue<Node<char>>();
startingNode.Weight = 0;
queue.Enqueue(startingNode);
while (queue.Count > 0)
{
Node<char> currentNode = queue.Dequeue();
visitedNodes.Add(currentNode.Symbol);
foreach (var connection in currentNode.Connections)
{
Node<char> toNode = connection.ToNode;
if (!visitedNodes.Contains(toNode.Symbol))
{
long temporaryWeight = currentNode.Weight + connection.Distance;
if (temporaryWeight < toNode.Weight)
{
toNode.Weight = temporaryWeight;
queue.Enqueue(toNode);
paths[toNode.Symbol] = paths[currentNode.Symbol] + " -> " +
toNode.Symbol + "(" + toNode.Weight + ")";
}
}
}
}
return paths;
}
示例15: Test_Dequeue
public void Test_Dequeue()
{
var pq = new PriorityQueue<int>();
items.ForEach(item => pq.Enqueue(item));
items.OrderBy(x => x).ToList().ForEach(item => Assert.AreEqual(pq.Dequeue(), item));
}