-
Notifications
You must be signed in to change notification settings - Fork 68
/
Queue.cs
118 lines (105 loc) · 3.02 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections;
using System.Linq;
namespace QueueApp
{
public class Queue<T>
{
private readonly int _capacity;
private int _size;
private T[] _internalArray;
public Queue(int capacity)
{
if (capacity <= 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity));
}
_capacity = capacity;
_internalArray = new T[capacity];
rear = _capacity - 1;
}
public bool IsEmpty => _size == 0;
public bool IsFull => _size == _capacity;
int rear { get; set; }
int front { get; set; }
public int Enqueue(T item)
{
if (IsFull)
throw new Exception("No space left!");
rear = (rear + 1) % _capacity;
_internalArray[rear] = item;
return ++_size;
}
public T Dequeue()
{
if (IsEmpty)
throw new Exception("No items int the queue!");
T result = _internalArray[front];
_internalArray[front] = default;
front = (front + 1) % _capacity;
_size--;
return result;
}
public T Front => IsEmpty ? default : _internalArray[front];
public T Rear => IsEmpty ? default : _internalArray[rear];
}
class a
{
public int v;
}
class Program
{
static void AddToQueue<T>(Queue<T> queue, T i)
{
try
{
queue.Enqueue(i);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine($"Full {queue.IsFull} Rear {queue.Rear} Front {queue.Front}");
}
static void RemoveFromQueue<T>(Queue<T> queue)
{
try
{
T item = queue.Dequeue();
Console.WriteLine($"dequeued {item}");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine($"Full {queue.IsFull} Rear {queue.Rear} Front {queue.Front}");
}
static void f(System.Collections.Generic.IEnumerable<a> l)
{
foreach(var p in l)
{
p.v *= -1;
}
}
static void Main(string[] args)
{
var queue = new Queue<int>(5);
AddToQueue(queue, 1);
AddToQueue(queue, 5);
AddToQueue(queue, 7);
AddToQueue(queue, 3);
AddToQueue(queue, 2);
AddToQueue(queue, 8);
RemoveFromQueue(queue);
AddToQueue(queue, 8);
var aa = new System.Collections.Generic.List<a> { new a { v = -1 }, new a { v = -2 } };
f(aa.Skip(0).Take(1));
f(aa.Skip(1).Take(1));
foreach(var p in aa)
{
Console.WriteLine($"{p.v}");
}
Console.ReadKey();
}
}
}