Skip to content

Commit

Permalink
UniqueList
Browse files Browse the repository at this point in the history
  • Loading branch information
kamendov-maxim committed Mar 24, 2024
1 parent 2e7b4c9 commit 6da8d4a
Show file tree
Hide file tree
Showing 11 changed files with 592 additions and 0 deletions.
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; }
}
214 changes: 214 additions & 0 deletions UniqueList/List.Src/List.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
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="OutOfRangeException">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 OutOfRangeException();
}

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);
head.Previous = newElement;

Check warning on line 68 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 68 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.

Check warning on line 68 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 68 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 68 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 68 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.
newElement.Next = head;
head = newElement;
++Count;
return;
}

var currenElement = FindNode(index);
newElement.Next = currenElement;
newElement.Previous = currenElement.Previous;
currenElement.Previous.Next = newElement;

Check warning on line 78 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 78 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.

Check warning on line 78 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 78 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 78 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 78 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.
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)
{
head.Next = newElement;

Check warning on line 97 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 97 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.

Check warning on line 97 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 97 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 97 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 97 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.
newElement.Previous = head;
}
else
{
bottom.Next = newElement;

Check warning on line 102 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 102 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.

Check warning on line 102 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 102 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 102 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 102 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.
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)
{
output[i] = currenElement.Value;

Check warning on line 119 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 119 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.

Check warning on line 119 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 119 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 119 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 119 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.
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="OutOfRangeException">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 OutOfRangeException();
}

if (index == Count - 1)
{
if (Count == 0)
{
head = null;
bottom = null;
}
else
{
bottom = bottom.Previous;

Check warning on line 148 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 148 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.

Check warning on line 148 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 148 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 148 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 148 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.
}
}

int value;

if (index == 0)
{
value = head.Value;

Check warning on line 156 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 156 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.

Check warning on line 156 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 156 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 156 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 156 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.
head = head.Next;
--Count;
return value;
}

var element = FindNode(index);
value = element.Value;
element.Next.Previous = element.Previous;

Check warning on line 164 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 164 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.

Check warning on line 164 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 164 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 164 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 164 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.
element.Previous.Next = element.Next;

Check warning on line 165 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 165 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.

Check warning on line 165 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 165 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 165 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Dereference of a possibly null reference.

Check warning on line 165 in UniqueList/List.Src/List.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu_and_MacOs (macos-latest)

Dereference of a possibly null reference.
--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="OutOfRangeException">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 OutOfRangeException("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)
{
currenElement = currenElement.Next;
}
}
else
{
currenElement = bottom;
for (int i = Count - 1; i > index; --i)
{
currenElement = currenElement.Previous;
}
}

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>
9 changes: 9 additions & 0 deletions UniqueList/List.Src/OutOfRangeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Lists;

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

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



class Program
{
public static void Main()
{
string options = """
What kind of list do you want to create? Write number to console to choose
0 - List
1 - UniqueList
""";

Console.WriteLine(options);
int listType;
while (!int.TryParse(Console.ReadLine(), out listType) || listType > 1 || listType < 0)
{
Console.WriteLine("Incorrect input");
Console.Write(options);
}
Console.WriteLine();

IList list;
if (listType == 0)
{
list = new List();
}
else
{
list = new UniqueList();
}

bool running = true;
while (running)
{
options = """
0 - Exit
1 - Add element to list
2 - Insert element to list
3 - Remove element from list
4 - Print list
Write number to console to choose option
""";

Console.WriteLine(options);

int option;
while (!int.TryParse(Console.ReadLine(), out option) || option > 4 || option < 0)
{
Console.WriteLine("Incorrect input");
Console.WriteLine(options);
}

switch (option)
{
case 0:
{
running = false;
break;
}
case 1:
{
var value = UserInputTryParse("Write value you want to add");
try
{
list.Add(value);
}
catch (ElementExistsException)
{
Console.WriteLine("You have already added this element earlier");
}
break;
}
case 2:
{
var index = UserInputTryParse("Write an index where you want to insert an element");
var value = UserInputTryParse("Write value you want to insert");

try
{
list.Insert(index, value);
}
catch (OutOfRangeException)
{
Console.WriteLine("Index is out of range");
}
catch (ElementExistsException)
{
Console.WriteLine("You have already added this element earlier");
}
break;
}
case 3:
{
var index = UserInputTryParse("Write an index where you want to delete an element");
try
{
list.RemoveAt(index);
}
catch (OutOfRangeException)
{
Console.WriteLine("Index is out of range");
}
break;
}
case 4:
{
Console.Write("[");
for (int i = 0; i < list.Count; ++i)
{
Console.Write(list.FindValue(i));
if (i != list.Count - 1)
{
Console.Write(", ");
}
}
Console.Write("]\n");
break;
}
}
Console.WriteLine();
}
}

private static int UserInputTryParse(string message)
{
Console.WriteLine(message);
int userInput;
while (!int.TryParse(Console.ReadLine(), out userInput))
{
Console.WriteLine("Incorrect input");
Console.WriteLine(message);
}

return userInput;
}
}
Loading

0 comments on commit 6da8d4a

Please sign in to comment.