Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UniqueList #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions UniqueList/List.Src/ElementExistsException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Lists;

public class ElementExistsException: Exception
{
public ElementExistsException() { }
public ElementExistsException(string message) : base(message) { }

public ElementExistsException(string message, Exception innerException) : base(message, innerException) { }
}
11 changes: 11 additions & 0 deletions UniqueList/List.Src/IList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Lists;

public interface IList
{
void Add(int value);
void Insert(int index, int value);
int[] ToArray();
int RemoveAt(int index);
int FindValue(int index);
int Count { get; }
}
264 changes: 264 additions & 0 deletions UniqueList/List.Src/List.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
using System.Data.Common;
using System.Dynamic;
using System.Reflection.Metadata.Ecma335;
using System.Runtime.CompilerServices;
using System.Threading.Tasks.Dataflow;
using System.Xml;

namespace Lists;

/// <summary>
/// An implementation of linked list
/// </summary>
public class List : IList
{
protected class ListElement
{
public ListElement? Next;
public ListElement? Previous;
public int Value;
public ListElement(int value)
{
Value = value;

Next = null;
Previous = null;
}
}

public List()
{
Count = 0;
}

/// <summary>
/// Method to insert value at given index
/// </summary>
/// <param name="index">Where to insert value</param>
/// <param name="value">Value to insert</param>
/// <exception cref="IndexOutOfRangeException">Exception is thrown if index is less than 0 or
/// bigger than list.Count and it is impossible to insert value</exception>
public virtual void Insert(int index, int value)
{
if (index > Count || index < 0)
{
throw new IndexOutOfRangeException();
}

var newElement = new ListElement(value);

if (index == Count)
{
if (Count == 0)
{
head = newElement;
}
if (bottom is not null)
{
newElement.Previous = bottom;
bottom.Next = newElement;
}
bottom = newElement;
++Count;
return;
}

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;
++Count;
return;
}

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;
}

/// <summary>
/// Adds value to the end of list
/// </summary>
/// <param name="value">Value to add</param>
public virtual void Add(int value)
{
var newElement = new ListElement(value);
if (Count == 0)
{
head = newElement;
bottom = newElement;
}
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;
}
bottom = newElement;
++Count;
}

/// <summary>
/// Method to get an int array containing all the values from list in the same order
/// </summary>
/// <returns>Array with values</returns>
public int[] ToArray()
{
var output = new int[Count];
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;
}

return output;
}

/// <summary>
/// Method allowing to remove value at given index
/// </summary>
/// <param name="index">Index of element to remove</param>
/// <returns>Value of this element</returns>
/// <exception cref="IndexOutOfRangeException">Thrown if there is no element with such index in the list</exception>
public int RemoveAt(int index)
{
if (index > Count - 1 || index < 0 || Count == 0)
{
throw new IndexOutOfRangeException();
}

if (index == Count - 1)
{
if (Count == 0)
{
head = null;
bottom = null;
}
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;
return value;
}

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;
return value;
}

/// <summary>
/// Method to get a value of an element at a givent index
/// </summary>
/// <param name="index">Index of an element to find</param>
/// <returns>Value of an element</returns>
/// <exception cref="IndexOutOfRangeException">Thrown if there is no element with such index in the list</exception>
public int FindValue(int index)
{
if (index > Count - 1 || index < 0)
{
throw new IndexOutOfRangeException("Index is out of range");
}

var element = FindNode(index);

return element.Value;
}

protected ListElement FindNode(int index)
{
ListElement? currenElement;
if (index < Count / 2)
{
currenElement = head;
for (int i = 0; i < index; ++i)
{
if (currenElement is null)
{
throw new InvalidOperationException("List does not work correctly");
}
currenElement = currenElement.Next;
}
}
else
{
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; }
protected ListElement? head = null;
protected ListElement? bottom = null;
}
10 changes: 10 additions & 0 deletions UniqueList/List.Src/List.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Loading
Loading