Skip to content

Commit 3763bef

Browse files
committed
build fix
1 parent fe56136 commit 3763bef

File tree

5 files changed

+212
-0
lines changed

5 files changed

+212
-0
lines changed

src/Advanced.Algorithms/Advanced.Algorithms.Docs.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@
9494
<Compile Include="DataStructures\LinkedList\SinglyLinkedList.cs" />
9595
<Compile Include="DataStructures\List\ArrayList.cs" />
9696
<Compile Include="DataStructures\List\SkipList.cs" />
97+
<Compile Include="DataStructures\Queues\ArrayQueue.cs" />
98+
<Compile Include="DataStructures\Queues\LinkedListQueue.cs" />
99+
<Compile Include="DataStructures\Queues\PriorityQueue.cs" />
100+
<Compile Include="DataStructures\Queues\Queue.cs" />
97101
<Compile Include="DataStructures\Set\BloomFilter.cs" />
98102
<Compile Include="DataStructures\Set\DisJointSet.cs" />
99103
<Compile Include="DataStructures\Set\SparseSet.cs" />
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
4+
namespace Advanced.Algorithms.DataStructures.Foundation
5+
{
6+
internal class ArrayQueue<T> : IQueue<T>
7+
{
8+
private readonly List<T> list = new List<T>();
9+
10+
public int Count { get; private set; }
11+
12+
public void Enqueue(T item)
13+
{
14+
list.Insert(0, item);
15+
Count++;
16+
}
17+
18+
public T Dequeue()
19+
{
20+
if (list.Count == 0)
21+
{
22+
throw new System.Exception("Empty Queue");
23+
}
24+
25+
var result = list[list.Count - 1];
26+
list.RemoveAt(list.Count - 1);
27+
Count--;
28+
return result;
29+
}
30+
31+
public IEnumerator<T> GetEnumerator()
32+
{
33+
return GetEnumerator();
34+
}
35+
36+
IEnumerator IEnumerable.GetEnumerator()
37+
{
38+
return list.GetEnumerator();
39+
}
40+
}
41+
42+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
4+
namespace Advanced.Algorithms.DataStructures.Foundation
5+
{
6+
internal class LinkedListQueue<T> : IQueue<T>
7+
{
8+
private readonly DoublyLinkedList<T> list = new DoublyLinkedList<T>();
9+
10+
public int Count { get; private set; }
11+
12+
public void Enqueue(T item)
13+
{
14+
list.InsertFirst(item);
15+
Count++;
16+
}
17+
18+
public T Dequeue()
19+
{
20+
if (list.Head == null)
21+
{
22+
throw new System.Exception("Empty Queue");
23+
}
24+
25+
var result = list.DeleteLast();
26+
Count--;
27+
return result;
28+
}
29+
30+
public IEnumerator<T> GetEnumerator()
31+
{
32+
return GetEnumerator();
33+
}
34+
35+
IEnumerator IEnumerable.GetEnumerator()
36+
{
37+
return list.GetEnumerator();
38+
}
39+
}
40+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
namespace Advanced.Algorithms.DataStructures
6+
{
7+
/// <summary>
8+
/// A priority queue implementation using heap
9+
/// </summary>
10+
public class PriorityQueue<T> : IEnumerable<T> where T : IComparable
11+
{
12+
private readonly BHeap<T> heap;
13+
public PriorityQueue(bool isMax = false)
14+
{
15+
heap = new BHeap<T>(isMax);
16+
}
17+
18+
/// <summary>
19+
/// Time complexity:O(log(n)).
20+
/// </summary>
21+
public void Enqueue(T item)
22+
{
23+
heap.Insert(item);
24+
}
25+
26+
/// <summary>
27+
/// Time complexity:O(log(n)).
28+
/// </summary>
29+
public T Dequeue()
30+
{
31+
return heap.Extract();
32+
}
33+
34+
/// <summary>
35+
/// Time complexity:O(1).
36+
/// </summary>
37+
public T Peek()
38+
{
39+
return heap.Peek();
40+
}
41+
42+
public IEnumerator<T> GetEnumerator()
43+
{
44+
return GetEnumerator();
45+
}
46+
47+
IEnumerator IEnumerable.GetEnumerator()
48+
{
49+
return heap.GetEnumerator();
50+
}
51+
}
52+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
4+
namespace Advanced.Algorithms.DataStructures.Foundation
5+
{
6+
/// <summary>
7+
/// A queue implementation.
8+
/// </summary>
9+
public class Queue<T> : IEnumerable<T>
10+
{
11+
private readonly IQueue<T> queue;
12+
13+
/// <summary>
14+
/// The number of items in the queue.
15+
/// </summary>
16+
public int Count => queue.Count;
17+
18+
/// <param name="type">The queue implementation type.</param>
19+
public Queue(QueueType type = QueueType.Array)
20+
{
21+
if (type == QueueType.Array)
22+
{
23+
queue = new ArrayQueue<T>();
24+
}
25+
else
26+
{
27+
queue = new LinkedListQueue<T>();
28+
}
29+
}
30+
31+
/// <summary>
32+
/// Time complexity:O(1).
33+
/// </summary>
34+
public void Enqueue(T item)
35+
{
36+
queue.Enqueue(item);
37+
}
38+
39+
/// <summary>
40+
/// Time complexity:O(1).
41+
/// </summary>
42+
public T Dequeue()
43+
{
44+
return queue.Dequeue();
45+
}
46+
47+
public IEnumerator<T> GetEnumerator()
48+
{
49+
return GetEnumerator();
50+
}
51+
52+
IEnumerator IEnumerable.GetEnumerator()
53+
{
54+
return queue.GetEnumerator();
55+
}
56+
}
57+
58+
internal interface IQueue<T> : IEnumerable<T>
59+
{
60+
int Count { get; }
61+
void Enqueue(T item);
62+
T Dequeue();
63+
}
64+
65+
/// <summary>
66+
/// The queue implementation types.
67+
/// </summary>
68+
public enum QueueType
69+
{
70+
Array = 0,
71+
LinkedList = 1
72+
}
73+
74+
}

0 commit comments

Comments
 (0)