Skip to content

Instantly share code, notes, and snippets.

@ChrisMoney
Created February 15, 2012 14:47
Show Gist options
  • Save ChrisMoney/16a548c6ca15a09c426b to your computer and use it in GitHub Desktop.
Save ChrisMoney/16a548c6ca15a09c426b to your computer and use it in GitHub Desktop.
C# --Shipping Console Program
// PriorityQueue – demonstrates using lower-level queue collection objects
// (generic ones at that) to implement a higher-level generic queue that stores objects in priority order
using System;
using System.Collections.Generic;
namespace PriorityQueue
{
class Program
{
// Main – fill the priority queue with packages, then remove a random number of them
static void Main(string[] args)
{
Console.WriteLine(Create a priority queue:”);
PriorityQueue<Package> pq = new PriorityQueue<Package>();
Console.WriteLine(“Add a random number (0 – 20) of random packages to queue:”);
Package pack;
PackageFactory fact = new packageFactory();
// we want a random number less than 20
Random rand = new Random();
int numToCreate = rand.Next(20); // random int from 0 – 20
Console.WriteLine(“\tCreating {0} packages: “, numToCreate);
for (int I = 0; I < numToCreate; i++)
{
Console.Write(“\t\tGenerating and adding random package {0}”, i);
pack = fact.CreatePackage();
Console.WriteLine(“ with priority {0}”, pack Priority);
pq.Enqueue(pack);
}
Console.WriteLine(“See what we got:”);
int nTotal = pq.Count;
Console.WriteLine(“Packages received: {0}”, nTotal);
Console.WriteLine(“Remove random number of packages: 0 – 20:”)
int numToRemove = rand.Next(20);
Console.WriteLine(“\tRemoving up to {0} packages”, numToRemove);
for (int I = 0; i < numToRemove; i++)
{
pack = pq.Dequeue();
if (pack != null)
{
Console.WriteLine(“\t\tShipped package with priority {0}”, pack.Priority);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment