如何使用两个图像中所附的自定义接口来实现优先级队列?
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyQueue
{
public class PriorityQueue<T> where T : IEquatable<T>
{
private IDictionary<int, IList<T>> elements;
public PriorityQueue()
{
elements = new SortedDictionary<int, IList<T>>();
}
public PriorityQueue(IDictionary<int, IList<T>> elements) : this()
{
}
public int Count()
{
return elements.Count;
}
public bool Contains(T item)
{
bool res = false;
foreach (KeyValuePair<int, IList<T>> pair in elements)
{
enter image description here
if (pair.Value[0].Equals(item))
{
Console.Write($"{item} Found --> {pair.Key} {pair.Value[0]}");
res = true;
}
if (res == true)
{
return res;
}
}
return res;
}
public T Dequeue()
{
IList<T> list = elements[elements.Keys.First()];
int priority = elements.Keys.First();
T highestPriority = list.First();
list.Remove(highestPriority);
if (list.Count == 0)
{
elements.Remove(priority);
}
return highestPriority;
}
public void Enqueue(int priority, T item)
{
IList<T> items;
if (!elements.ContainsKey(priority))
elements.Add(priority, new List<T>());
items = elements[priority];
items.Add(item);
}
public T Peek()
{
IList<T> priorityList = elements[elements.Keys.First()];
return priorityList[0];
}
public int getHighestPriority()
{
int Firstkey = elements.Take(1).Select(d => d.Key).First();
return Firstkey;
}
}
class Program
{
static void Main(string[] args)
{
PriorityQueue<string> priorityQueue = new PriorityQueue<string>();
priorityQueue.Enqueue(3, "Iooawi");
priorityQueue.Enqueue(1, "Zuika");
priorityQueue.Enqueue(2, "Aki");
priorityQueue.Enqueue(5, "Iak");
Console.WriteLine($"Top item: -> { priorityQueue.Peek()}");
Console.WriteLine($"Removed item--> {priorityQueue.Dequeue()}");
Console.WriteLine($"Top item: -> { priorityQueue.Peek()}");
Console.WriteLine($"Items present in this queue {priorityQueue.Count()}");
Console.WriteLine($" ---> { priorityQueue.Contains("Iak")}");
Console.WriteLine($"{priorityQueue.getHighestPriority()}");
}
}
}
如何使用图像中给出的自定义接口实现以下实现,上面是我尝试过的代码以及如何添加字段到自定义界面。 我已尝试在上述实现中使用 Iequatable,但在自定义接口 Ipriority 中,它不允许我设置字段 int 优先级? 接口的实现方式有哪些?另外,如果我将 setHighestPriority 方法设置为私有,如何访问它?
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyQueue
{
public class PriorityQueue<T> where T : IEquatable<T>
{
private IDictionary<int, IList<T>> elements;
public PriorityQueue()
{
elements = new SortedDictionary<int, IList<T>>();
}
public PriorityQueue(IDictionary<int, IList<T>> elements) : this()
{
}
public int Count()
{
return elements.Count;
}
public bool Contains(T item)
{
bool res = false;
foreach (KeyValuePair<int, IList<T>> pair in elements)
{
enter image description here
if (pair.Value[0].Equals(item))
{
Console.Write(quot;{item} Found --> {pair.Key} {pair.Value[0]}");
res = true;
}
if (res == true)
{
return res;
}
}
return res;
}
public T Dequeue()
{
IList<T> list = elements[elements.Keys.First()];
int priority = elements.Keys.First();
T highestPriority = list.First();
list.Remove(highestPriority);
if (list.Count == 0)
{
elements.Remove(priority);
}
return highestPriority;
}
public void Enqueue(int priority, T item)
{
IList<T> items;
if (!elements.ContainsKey(priority))
elements.Add(priority, new List<T>());
items = elements[priority];
items.Add(item);
}
public T Peek()
{
IList<T> priorityList = elements[elements.Keys.First()];
return priorityList[0];
}
public int getHighestPriority()
{
int Firstkey = elements.Take(1).Select(d => d.Key).First();
return Firstkey;
}
}
class Program
{
static void Main(string[] args)
{
PriorityQueue<string> priorityQueue = new PriorityQueue<string>();
priorityQueue.Enqueue(3, "Iooawi");
priorityQueue.Enqueue(1, "Zuika");
priorityQueue.Enqueue(2, "Aki");
priorityQueue.Enqueue(5, "Iak");
Console.WriteLine(quot;Top item: -> { priorityQueue.Peek()}");
Console.WriteLine(quot;Removed item--> {priorityQueue.Dequeue()}");
Console.WriteLine(quot;Top item: -> { priorityQueue.Peek()}");
Console.WriteLine(quot;Items present in this queue {priorityQueue.Count()}");
Console.WriteLine(quot; ---> { priorityQueue.Contains("Iak")}");
Console.WriteLine(quot;{priorityQueue.getHighestPriority()}");
}
}
}
Priority Queue Implementation 2
Priority Queue Implementation 3
How do I achieve the following implementations using custom interfaces given in the images, above is the code which I have tried and how do I add the field to the custom interface.
I have tried using Iequatable in the above implementation but in the custom interface Ipriority it is not allowing me to set the field int priority?
What are the ways to implement interface? Also how to access setHighestPriority method if I set it to private?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论