Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.41 KB | None | 0 0
  1. using System;
  2.  
  3. namespace DailyProgrammer_Practial_201
  4. {
  5.     internal class Program
  6.     {
  7.         private static void Main()
  8.         {
  9.             var myQ = new PriorityQueue();
  10.  
  11.             myQ.Enqueue("Knife A5 B10", 5, 10);
  12.             myQ.Enqueue("Fork A15 B7", 15, 7);
  13.             myQ.Enqueue("Spoon A0.9 B11", 0.9, 11);
  14.             myQ.Enqueue("Glass A13.2 B22", 13.2, 22);
  15.             myQ.Enqueue("Plate A5 B6", 5, 6);
  16.             Console.WriteLine(myQ.DequeueA());
  17.             Console.WriteLine(myQ.DequeueA());
  18.             Console.WriteLine(myQ.DequeueB());
  19.             Console.WriteLine(myQ.DequeueB());
  20.             Console.WriteLine(myQ.DequeueA());
  21.         }
  22.     }
  23.  
  24.     internal class PriorityQueue
  25.     {
  26.         private QueueItem _first;
  27.  
  28.         public void Enqueue(string itemName, double priorityValueA, double priorityValueB)
  29.         {
  30.             var thisItem = new QueueItem(itemName, priorityValueA, priorityValueB);
  31.             if (_first == null) _first = thisItem;
  32.             else
  33.             {
  34.                 var tmp = _first;
  35.                 while (tmp.NextQueueItem != null)
  36.                 {
  37.                     tmp = tmp.NextQueueItem;
  38.                 }
  39.                 tmp.NextQueueItem = thisItem;
  40.             }
  41.         }
  42.  
  43.         public string DequeueA()
  44.         {
  45.             if (_first == null)
  46.             {
  47.                 return null;
  48.             }
  49.             var highestPriorityItem = _first;
  50.             QueueItem beforeHighestPriorityQueueItem = null;
  51.             var tmpQueueItem = _first;
  52.             QueueItem lastQueueItem = null;
  53.  
  54.  
  55.             while (tmpQueueItem != null)
  56.             {
  57.                 if (tmpQueueItem.A > highestPriorityItem.A)
  58.                 {
  59.                     beforeHighestPriorityQueueItem = lastQueueItem;
  60.                     highestPriorityItem = tmpQueueItem;
  61.                 }
  62.                 lastQueueItem = tmpQueueItem;
  63.                 tmpQueueItem = tmpQueueItem.NextQueueItem;
  64.             }
  65.             if (highestPriorityItem.NextQueueItem != null && beforeHighestPriorityQueueItem != null)
  66.             {
  67.                 beforeHighestPriorityQueueItem.NextQueueItem = highestPriorityItem.NextQueueItem;
  68.             }
  69.             else if (beforeHighestPriorityQueueItem != null)
  70.             {
  71.                 beforeHighestPriorityQueueItem.NextQueueItem = null;
  72.             }
  73.             else
  74.             {
  75.                 _first = highestPriorityItem.NextQueueItem;
  76.             }
  77.             return highestPriorityItem.Name;
  78.         }
  79.  
  80.         public string DequeueB()
  81.         {
  82.             if (_first == null)
  83.             {
  84.                 return null;
  85.             }
  86.             var highestPriorityItem = _first;
  87.             QueueItem beforeHighestPriorityQueueItem = null;
  88.             var tmpQueueItem = _first;
  89.             QueueItem lastQueueItem = null;
  90.  
  91.  
  92.             while (tmpQueueItem != null)
  93.             {
  94.                 if (tmpQueueItem.B > highestPriorityItem.B)
  95.                 {
  96.                     beforeHighestPriorityQueueItem = lastQueueItem;
  97.                     highestPriorityItem = tmpQueueItem;
  98.                 }
  99.                 lastQueueItem = tmpQueueItem;
  100.                 tmpQueueItem = tmpQueueItem.NextQueueItem;
  101.             }
  102.             if (highestPriorityItem.NextQueueItem != null && beforeHighestPriorityQueueItem != null)
  103.             {
  104.                 beforeHighestPriorityQueueItem.NextQueueItem = highestPriorityItem.NextQueueItem;
  105.             }
  106.             else if (beforeHighestPriorityQueueItem != null)
  107.             {
  108.                 beforeHighestPriorityQueueItem.NextQueueItem = null;
  109.             }
  110.             else
  111.             {
  112.                 _first = highestPriorityItem.NextQueueItem;
  113.             }
  114.             return highestPriorityItem.Name;
  115.         }
  116.  
  117.         public int Count()
  118.         {
  119.             var count = 0;
  120.             var tmp = _first;
  121.             while (tmp != null)
  122.             {
  123.                 count++;
  124.                 tmp = tmp.NextQueueItem;
  125.             }
  126.             return count;
  127.         }
  128.  
  129.         public void Clear()
  130.         {
  131.             _first = null;
  132.         }
  133.  
  134.         private class QueueItem
  135.         {
  136.             public readonly double A;
  137.             public readonly double B;
  138.             public readonly string Name;
  139.             public QueueItem NextQueueItem { get; set; }
  140.  
  141.             public QueueItem(string name, double a, double b)
  142.             {
  143.                 Name = name;
  144.                 A = a;
  145.                 B = b;
  146.             }
  147.         }
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement