Updated Index checking bound on Linked List
This commit is contained in:
parent
dc2768f1ac
commit
02f49782ee
@ -45,17 +45,22 @@ namespace C_.Datastructures
|
|||||||
public T? this[int i]
|
public T? this[int i]
|
||||||
{
|
{
|
||||||
get {
|
get {
|
||||||
Node<T> node = Traverse(i);
|
//Check Range
|
||||||
|
if (i >= Count) throw new System.Exception("Error! Index out of Bounds");
|
||||||
|
|
||||||
|
//Return Value
|
||||||
|
Node<T>? node = Traverse(i);
|
||||||
if (node != null) return node.Value;
|
if (node != null) return node.Value;
|
||||||
return default(T);
|
return default(T);
|
||||||
}
|
}
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (i < Count)
|
//Check Range
|
||||||
{
|
if (i >= Count) throw new System.Exception("Error! Index out of Bounds");
|
||||||
Node<T> node = Traverse(i);
|
|
||||||
node.Value = value;
|
//Change Value
|
||||||
}
|
Node<T>? node = Traverse(i);
|
||||||
|
node!.Value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,5 +19,5 @@ list2.Append(3);
|
|||||||
|
|
||||||
LinkedList<int> list3 = LinkedList<int>.Create(list, list2);
|
LinkedList<int> list3 = LinkedList<int>.Create(list, list2);
|
||||||
|
|
||||||
int x = list3[2] = 5;
|
int x = list3[5] = 5;
|
||||||
Console.ReadLine();
|
Console.ReadLine();
|
Loading…
Reference in New Issue
Block a user