-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathQueue.cs
47 lines (40 loc) · 1.03 KB
/
Queue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
namespace PracticeQuestionsSharp.DataStructures
{
//Generic implementation of a queue using my linked list implementation.
// Code is largely the same as stack implementation.
public class Queue<T>
{
public Queue()
{
list = new LinkedList<T>();
Count = 0;
}
public Queue<T> Enqueue(T data)
{
list.Add(data);
Count++;
return this;
}
public T Dequeue()
{
if (IsEmpty) throw new InvalidOperationException("Queue empty.");
T result = list.Head.Data;
list.RemoveNode(list.Head);
Count--;
return result;
}
public T Peek()
{
return list.Head.Data;
}
public void Clear()
{
list.Clear();
Count = 0;
}
public bool IsEmpty => Count == 0;
public int Count { get; private set; }
private readonly LinkedList<T> list;
}
}