Skip to content

Commit 34cf5cb

Browse files
NickPolyderaalhour
authored andcommitted
Use Generic collection for Queue<T> (aalhour#67)
1 parent d11baed commit 34cf5cb

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

DataStructures/Lists/Queue.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class Queue<T> : IEnumerable<T> where T : IComparable<T>
1616
private int _tailPointer { get; set; }
1717

1818
// The internal collection.
19-
private object[] _collection { get; set; }
19+
private T[] _collection { get; set; }
2020
private const int _defaultCapacity = 8;
2121

2222
// This sets the default maximum array length to refer to MAXIMUM_ARRAY_LENGTH_x64
@@ -45,7 +45,7 @@ public Queue(int initialCapacity)
4545
_size = 0;
4646
_headPointer = 0;
4747
_tailPointer = 0;
48-
_collection = new object[initialCapacity];
48+
_collection = new T[initialCapacity];
4949
}
5050

5151

@@ -77,7 +77,7 @@ private void _resize(int newSize)
7777
{
7878
//Array.Resize (ref _collection, newSize);
7979

80-
var tempCollection = new object[newSize];
80+
var tempCollection = new T[newSize];
8181
Array.Copy(_collection, _headPointer, tempCollection, 0, _size);
8282
_collection = tempCollection;
8383
}
@@ -123,7 +123,7 @@ public T Top
123123
if (IsEmpty)
124124
throw new Exception("Queue is empty.");
125125

126-
return (T)_collection[_headPointer];
126+
return _collection[_headPointer];
127127
}
128128
}
129129

@@ -166,7 +166,7 @@ public T Dequeue()
166166
throw new Exception("Queue is empty.");
167167

168168
var topItem = _collection[_headPointer];
169-
_collection[_headPointer] = null;
169+
_collection[_headPointer] = default(T);
170170

171171
// Decrement the size
172172
_size--;
@@ -193,7 +193,7 @@ public T Dequeue()
193193
_tailPointer = Array.IndexOf(_collection, tail);
194194
}
195195

196-
return (T)topItem;
196+
return topItem;
197197
}
198198

199199
/// <summary>
@@ -207,7 +207,7 @@ public T[] ToArray()
207207
int j = 0;
208208
for (int i = 0; i < _size; ++i)
209209
{
210-
array[j] = (T)_collection[_headPointer + i];
210+
array[j] = _collection[_headPointer + i];
211211
j++;
212212
}
213213

0 commit comments

Comments
 (0)