# Priority Queue

```csharp
public class PriorityQueue<T>
{
    private readonly SortedDictionary<int, Queue<T>> queue = [];

    public void Enqueue(int priority, T item)
    {
        if (!queue.ContainsKey(priority))
        {
            queue.Add(priority, new Queue<T>());
        }
        queue[priority].Enqueue(item);
    }

    public T Dequeue()
    {
        if (queue.Count == 0)
        {
            return default!;
        }

        var topPriority = GetHightPriorty();

        var subqueue = queue[topPriority];

        var item = subqueue.Dequeue();

        if (subqueue.Count == 0)
        {
            queue.Remove(topPriority);
        }

        return item;
    }


    public int Count()
    {
        int count = 0;
        foreach (var item in queue)
        {
            count += item.Value.Count;

        }

        return count;
    }

    public bool IsEmpty => queue.Count == 0;

    private int GetHightPriorty()
    {
        var enurmator = queue.GetEnumerator();
        enurmator.MoveNext();
        return enurmator.Current.Key;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-57.gitbook.io/data-structure-and-algorithms/data-structure/priority-queue.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
