Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace DailyProgrammer_Practial_201
- {
- internal class Program
- {
- private static void Main()
- {
- var myQ = new PriorityQueue();
- myQ.Enqueue("Knife A5 B10", 5, 10);
- myQ.Enqueue("Fork A15 B7", 15, 7);
- myQ.Enqueue("Spoon A0.9 B11", 0.9, 11);
- myQ.Enqueue("Glass A13.2 B22", 13.2, 22);
- myQ.Enqueue("Plate A5 B6", 5, 6);
- Console.WriteLine(myQ.DequeueA());
- Console.WriteLine(myQ.DequeueA());
- Console.WriteLine(myQ.DequeueB());
- Console.WriteLine(myQ.DequeueB());
- Console.WriteLine(myQ.DequeueA());
- }
- }
- internal class PriorityQueue
- {
- private QueueItem _first;
- public void Enqueue(string itemName, double priorityValueA, double priorityValueB)
- {
- var thisItem = new QueueItem(itemName, priorityValueA, priorityValueB);
- if (_first == null) _first = thisItem;
- else
- {
- var tmp = _first;
- while (tmp.NextQueueItem != null)
- {
- tmp = tmp.NextQueueItem;
- }
- tmp.NextQueueItem = thisItem;
- }
- }
- public string DequeueA()
- {
- if (_first == null)
- {
- return null;
- }
- var highestPriorityItem = _first;
- QueueItem beforeHighestPriorityQueueItem = null;
- var tmpQueueItem = _first;
- QueueItem lastQueueItem = null;
- while (tmpQueueItem != null)
- {
- if (tmpQueueItem.A > highestPriorityItem.A)
- {
- beforeHighestPriorityQueueItem = lastQueueItem;
- highestPriorityItem = tmpQueueItem;
- }
- lastQueueItem = tmpQueueItem;
- tmpQueueItem = tmpQueueItem.NextQueueItem;
- }
- if (highestPriorityItem.NextQueueItem != null && beforeHighestPriorityQueueItem != null)
- {
- beforeHighestPriorityQueueItem.NextQueueItem = highestPriorityItem.NextQueueItem;
- }
- else if (beforeHighestPriorityQueueItem != null)
- {
- beforeHighestPriorityQueueItem.NextQueueItem = null;
- }
- else
- {
- _first = highestPriorityItem.NextQueueItem;
- }
- return highestPriorityItem.Name;
- }
- public string DequeueB()
- {
- if (_first == null)
- {
- return null;
- }
- var highestPriorityItem = _first;
- QueueItem beforeHighestPriorityQueueItem = null;
- var tmpQueueItem = _first;
- QueueItem lastQueueItem = null;
- while (tmpQueueItem != null)
- {
- if (tmpQueueItem.B > highestPriorityItem.B)
- {
- beforeHighestPriorityQueueItem = lastQueueItem;
- highestPriorityItem = tmpQueueItem;
- }
- lastQueueItem = tmpQueueItem;
- tmpQueueItem = tmpQueueItem.NextQueueItem;
- }
- if (highestPriorityItem.NextQueueItem != null && beforeHighestPriorityQueueItem != null)
- {
- beforeHighestPriorityQueueItem.NextQueueItem = highestPriorityItem.NextQueueItem;
- }
- else if (beforeHighestPriorityQueueItem != null)
- {
- beforeHighestPriorityQueueItem.NextQueueItem = null;
- }
- else
- {
- _first = highestPriorityItem.NextQueueItem;
- }
- return highestPriorityItem.Name;
- }
- public int Count()
- {
- var count = 0;
- var tmp = _first;
- while (tmp != null)
- {
- count++;
- tmp = tmp.NextQueueItem;
- }
- return count;
- }
- public void Clear()
- {
- _first = null;
- }
- private class QueueItem
- {
- public readonly double A;
- public readonly double B;
- public readonly string Name;
- public QueueItem NextQueueItem { get; set; }
- public QueueItem(string name, double a, double b)
- {
- Name = name;
- A = a;
- B = b;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement