Exemple #1
0
        // additional test
        private static void TopInts()
        {
            int[] allInts = { 12, 11, 8, 7, 9, 5, 4, 3, 2, 29, 23, 1, 24, 30, 9, 4, 88, 5, 100, 29, 23, 5, 99, 87, 22, 111 };
            //MinPQ<int> pq0 = new MinPQ<int>(allInts);
            int         M  = allInts.Length / 3;
            MinPQ <int> pq = new MinPQ <int>(M + 1);

            Console.WriteLine("Top {0} is ", M);
            foreach (var n in allInts)
            {
                pq.Insert(n);
                Console.WriteLine("Min is {0}", pq.Min);
                // remove minimum if M+1 entries on the PQ
                if (pq.Count > M)
                {
                    pq.DelMin();
                }
            }
            // print entries on PQ in reverse order
            Stack <int> stack = new Stack <int>();

            foreach (int n in pq)
            {
                stack.Push(n);
            }
            foreach (int n in stack)
            {
                Console.WriteLine(n);
            }
            Console.WriteLine("These are the top elements");
        }
Exemple #2
0
        public static void MainTest(string[] args)
        {
            TextInput      StdIn = new TextInput();
            MinPQ <string> pq    = new MinPQ <string>();

            while (!StdIn.IsEmpty)
            {
                string item = StdIn.ReadString();
                if (!item.Equals("-"))
                {
                    pq.Insert(item);
                }
                else if (!pq.IsEmpty)
                {
                    Console.Write(pq.DelMin() + " ");
                }
            }
            Console.WriteLine("(" + pq.Count + " left on pq)");
        }