当前位置: 首页>>代码示例>>C#>>正文


C# PriorityQueue.Dequeue方法代码示例

本文整理汇总了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);
                    }
                }
            }
        }
开发者ID:Ivan-Dimitrov-bg,项目名称:.Net-framework,代码行数:29,代码来源:PriorityQueueTest.cs

示例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);
            }
        }
开发者ID:VelizarIT,项目名称:TelerikAcademy,代码行数:25,代码来源:Program.cs

示例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()));
    }
开发者ID:staafl,项目名称:ta-hw-dsa,代码行数:35,代码来源:program.cs

示例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());
        }
开发者ID:vladislav-karamfilov,项目名称:TelerikAcademy,代码行数:27,代码来源:EnqueueOperationTests.cs

示例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);
 }
开发者ID:elenazab,项目名称:Homework,代码行数:10,代码来源:UnitTest1.cs

示例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());
        }
开发者ID:peterkirilov,项目名称:SoftUni-1,代码行数:11,代码来源:TestSMain.cs

示例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());
        }
开发者ID:Novkirishki,项目名称:Data-Structures-and-Algorithms,代码行数:12,代码来源:Startup.cs

示例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);
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:49,代码来源:Enqueue.cs

示例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();
 }
开发者ID:kalinnikol,项目名称:TelerikAcademy-1,代码行数:15,代码来源:Program.cs

示例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");
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:15,代码来源:DefaultPriority.cs

示例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);
        }
开发者ID:nlehtola,项目名称:ict,代码行数:28,代码来源:PriorityQueueTests.cs

示例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());
    }
开发者ID:aleks-todorov,项目名称:HomeWorks,代码行数:20,代码来源:Program.cs

示例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);
                }
            }
        }
    }
开发者ID:MarKamenov,项目名称:TelerikAcademy,代码行数:28,代码来源:Dijkstra.cs

示例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;
    }
开发者ID:RamiAmaire,项目名称:TelerikAcademy,代码行数:33,代码来源:Demo.cs

示例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));
    }
开发者ID:MarKamenov,项目名称:TelerikAcademy,代码行数:7,代码来源:PriorityQueueTests.cs


注:本文中的PriorityQueue.Dequeue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。