diff --git a/UniqueList/List.Src/List.cs b/UniqueList/List.Src/List.cs
index 0da5a20..7c06cee 100644
--- a/UniqueList/List.Src/List.cs
+++ b/UniqueList/List.Src/List.cs
@@ -1,3 +1,4 @@
+using System.Data.Common;
using System.Dynamic;
using System.Reflection.Metadata.Ecma335;
using System.Runtime.CompilerServices;
@@ -9,7 +10,7 @@ namespace Lists;
///
/// An implementation of linked list
///
-public class List: IList
+public class List : IList
{
protected class ListElement
{
@@ -35,13 +36,13 @@ public List()
///
/// Where to insert value
/// Value to insert
- /// Exception is thrown if index is less than 0 or
+ /// Exception is thrown if index is less than 0 or
/// bigger than list.Count and it is impossible to insert value
public virtual void Insert(int index, int value)
{
if (index > Count || index < 0)
{
- throw new OutOfRangeException();
+ throw new IndexOutOfRangeException();
}
var newElement = new ListElement(value);
@@ -65,6 +66,12 @@ public virtual void Insert(int index, int value)
if (index == 0)
{
newElement = new ListElement(value);
+
+ if (head is null)
+ {
+ throw new InvalidOperationException("List does not work correctly");
+ }
+
head.Previous = newElement;
newElement.Next = head;
head = newElement;
@@ -75,6 +82,10 @@ public virtual void Insert(int index, int value)
var currenElement = FindNode(index);
newElement.Next = currenElement;
newElement.Previous = currenElement.Previous;
+ if (currenElement.Previous is null)
+ {
+ throw new InvalidOperationException("List does not work correctly");
+ }
currenElement.Previous.Next = newElement;
currenElement.Previous = newElement;
++Count;
@@ -94,11 +105,21 @@ public virtual void Add(int value)
}
else if (Count == 1)
{
+
+ if (head is null)
+ {
+ throw new InvalidOperationException("List does not work correctly");
+ }
+
head.Next = newElement;
newElement.Previous = head;
}
else
{
+ if (bottom is null)
+ {
+ throw new InvalidOperationException("List does not work correctly");
+ }
bottom.Next = newElement;
newElement.Previous = bottom;
}
@@ -116,6 +137,10 @@ public int[] ToArray()
var currenElement = head;
for (int i = 0; i < Count; ++i)
{
+ if (currenElement is null)
+ {
+ throw new InvalidOperationException("List does not work correctly");
+ }
output[i] = currenElement.Value;
currenElement = currenElement.Next;
}
@@ -128,12 +153,12 @@ public int[] ToArray()
///
/// Index of element to remove
/// Value of this element
- /// Thrown if there is no element with such index in the list
+ /// Thrown if there is no element with such index in the list
public int RemoveAt(int index)
{
if (index > Count - 1 || index < 0 || Count == 0)
{
- throw new OutOfRangeException();
+ throw new IndexOutOfRangeException();
}
if (index == Count - 1)
@@ -145,14 +170,22 @@ public int RemoveAt(int index)
}
else
{
+ if (bottom is null)
+ {
+ throw new InvalidOperationException("List does not work correctly");
+ }
bottom = bottom.Previous;
}
}
int value;
-
+
if (index == 0)
{
+ if (head is null)
+ {
+ throw new InvalidOperationException("List does not work correctly");
+ }
value = head.Value;
head = head.Next;
--Count;
@@ -161,6 +194,10 @@ public int RemoveAt(int index)
var element = FindNode(index);
value = element.Value;
+ if (element.Next is null || element.Previous is null)
+ {
+ throw new InvalidOperationException("List does not work correctly");
+ }
element.Next.Previous = element.Previous;
element.Previous.Next = element.Next;
--Count;
@@ -172,12 +209,12 @@ public int RemoveAt(int index)
///
/// Index of an element to find
/// Value of an element
- /// Thrown if there is no element with such index in the list
+ /// Thrown if there is no element with such index in the list
public int FindValue(int index)
{
if (index > Count - 1 || index < 0)
{
- throw new OutOfRangeException("Index is out of range");
+ throw new IndexOutOfRangeException("Index is out of range");
}
var element = FindNode(index);
@@ -193,6 +230,10 @@ protected ListElement FindNode(int index)
currenElement = head;
for (int i = 0; i < index; ++i)
{
+ if (currenElement is null)
+ {
+ throw new InvalidOperationException("List does not work correctly");
+ }
currenElement = currenElement.Next;
}
}
@@ -201,14 +242,23 @@ protected ListElement FindNode(int index)
currenElement = bottom;
for (int i = Count - 1; i > index; --i)
{
+ if (currenElement is null)
+ {
+ throw new InvalidOperationException("List does not work correctly");
+ }
currenElement = currenElement.Previous;
}
}
+ if (currenElement is null)
+ {
+ throw new InvalidOperationException("List does not work correctly");
+ }
+
return currenElement;
}
- public int Count {get; private set; }
+ public int Count { get; private set; }
protected ListElement? head = null;
protected ListElement? bottom = null;
}
diff --git a/UniqueList/List.Src/OutOfRangeException.cs b/UniqueList/List.Src/OutOfRangeException.cs
deleted file mode 100644
index d7ea64d..0000000
--- a/UniqueList/List.Src/OutOfRangeException.cs
+++ /dev/null
@@ -1,9 +0,0 @@
-namespace Lists;
-
-public class OutOfRangeException : Exception
-{
- public OutOfRangeException() { }
- public OutOfRangeException(string message) : base(message) { }
-
- public OutOfRangeException(string message, Exception innerException) : base(message, innerException) { }
-}
diff --git a/UniqueList/List.Tests/List.Tests.cs b/UniqueList/List.Tests/List.Tests.cs
index 5a1d4d4..04581ba 100644
--- a/UniqueList/List.Tests/List.Tests.cs
+++ b/UniqueList/List.Tests/List.Tests.cs
@@ -35,14 +35,14 @@ public void InsertTest()
public void TestOutOfRangeCase()
{
var list = new List();
- Assert.Throws(() => list.Insert(10, 6));
- Assert.Throws(() => list.Insert(-10, 6));
+ Assert.Throws(() => list.Insert(10, 6));
+ Assert.Throws(() => list.Insert(-10, 6));
- Assert.Throws(() => list.RemoveAt(10));
- Assert.Throws(() => list.RemoveAt(-10));
+ Assert.Throws(() => list.RemoveAt(10));
+ Assert.Throws(() => list.RemoveAt(-10));
- Assert.Throws(() => list.FindValue(10));
- Assert.Throws(() => list.FindValue(-10));
+ Assert.Throws(() => list.FindValue(10));
+ Assert.Throws(() => list.FindValue(-10));
}
[Test]