Repository navigation
Expand file tree
/
Copy pathCustomQueue.cs
More file actions
79 lines (67 loc) · 1.52 KB
/
Copy pathCustomQueue.cs
File metadata and controls
79 lines (67 loc) · 1.52 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
namespace HTMLParserApp
{
public class CustomQueue<T>
{
private class Node
{
public T Data { get; set; }
public Node Next { get; set; }
public Node(T data)
{
Data = data;
Next = null;
}
}
private Node front;
private Node rear;
public int Size { get; private set; }
public CustomQueue()
{
front = null;
rear = null;
Size = 0;
}
public void Enqueue(T item)
{
Node newNode = new Node(item);
if (rear == null)
{
front = rear = newNode;
}
else
{
rear.Next = newNode;
rear = newNode;
}
Size++;
}
public T Dequeue()
{
if (IsEmpty())
{
throw new InvalidOperationException("Queue is empty");
}
T item = front.Data;
front = front.Next;
if (front == null)
{
rear = null;
}
Size--;
return item;
}
public T Peek()
{
if (IsEmpty())
{
throw new InvalidOperationException("Queue is empty");
}
return front.Data;
}
public bool IsEmpty()
{
return Size == 0;
}
}
}