Skip to content
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
37 changes: 37 additions & 0 deletions ListAndUniqueList/ListAndUniqueList.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33403.182
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ListAndUniqueList", "ListAndUniqueList\ListAndUniqueList.csproj", "{7F7F9968-FB78-4353-AC51-E0D43F4A7557}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsList", "TestsList\TestsList.csproj", "{BB4EEB6F-6C72-4F81-A0BD-4AADA4AD9058}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsUniqueList", "TestsUniqueList\TestsUniqueList.csproj", "{6FA174A8-43FA-48FB-B3B7-E747E92E4A66}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7F7F9968-FB78-4353-AC51-E0D43F4A7557}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7F7F9968-FB78-4353-AC51-E0D43F4A7557}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7F7F9968-FB78-4353-AC51-E0D43F4A7557}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7F7F9968-FB78-4353-AC51-E0D43F4A7557}.Release|Any CPU.Build.0 = Release|Any CPU
{BB4EEB6F-6C72-4F81-A0BD-4AADA4AD9058}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB4EEB6F-6C72-4F81-A0BD-4AADA4AD9058}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB4EEB6F-6C72-4F81-A0BD-4AADA4AD9058}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB4EEB6F-6C72-4F81-A0BD-4AADA4AD9058}.Release|Any CPU.Build.0 = Release|Any CPU
{6FA174A8-43FA-48FB-B3B7-E747E92E4A66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6FA174A8-43FA-48FB-B3B7-E747E92E4A66}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6FA174A8-43FA-48FB-B3B7-E747E92E4A66}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6FA174A8-43FA-48FB-B3B7-E747E92E4A66}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B598B7F0-1489-463C-B6BB-24CBDBDD7F83}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions ListAndUniqueList/ListAndUniqueList/InvalidItemException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ListAndUniqueList;

/// <summary>
/// Throw exception when item is already in unique list
/// </summary>
public class InvalidItemException : Exception {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо комментарии и к исключениям тоже

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ListAndUniqueList;

/// <summary>
/// Throw exception when the position is incorrect
/// </summary>
public class InvalidPositionException : Exception {}
227 changes: 227 additions & 0 deletions ListAndUniqueList/ListAndUniqueList/List.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
namespace ListAndUniqueList;

/// <summary>
/// A list for storing and interacting with int type elements
/// </summary>
public class List
{
private ListElement? Head;
private ListElement? Tail;

/// <summary>
/// Adding an item to the list
/// </summary>
virtual public void AddElement(int position, int value)
{
if (Head == null)
{
var item = new ListElement(value);
Head = item;
Tail = item;
++Head.SizeList;
}
else
{
++Head.SizeList;
ListElement item = new ListElement(value);
if (Tail == null)
{
throw new NullReferenceException();
}
if (position == 0)
{
var temp = Head.Next;
Head = item;
Head.Next = temp;
return;
}
if (position == Head.SizeList - 1)
{
Tail.Next = item;
Tail = item;
return;
}
var walker = Head;
int counter = 0;

while (walker != null && walker.Next != null && counter != position)
{
walker = walker.Next;
counter++;
}
if (walker == null || walker.Next == null)
{
throw new NullReferenceException();
}
else
{
var temp = walker.Next.Next;
walker.Next = item;
item.Next = temp;
}
}
}

/// <summary>
/// Deletes an item at the end of the list
/// </summary>
/// <exception cref="NullPointerException">Throws an exception when the list is empty</exception>
/// <returns>Element which was deleted</returns>
virtual public int RemoveElement(int position)
{
if (Head == null || Tail == null)
{
throw new NullListException();
}
if (position > Head.SizeList)
{
throw new NullReferenceException();
}
if (Tail == Head)
{
var itemFromTail = Tail.Value;
Tail = null;
Head = null;
return itemFromTail;
}
if (position == 0)
{
var itemFromHead = Head.Value;
Head = Head.Next;
return itemFromHead;
}
if (position == Head.SizeList)
{
var itemFromTail = Tail.Value;
var walkerForTail = Head;
while (walkerForTail != null && walkerForTail.Next != Tail)
{
walkerForTail = walkerForTail.Next;
}
if (walkerForTail == null)
{
throw new NullReferenceException();
}
Tail = walkerForTail;
walkerForTail.Next = null;
}

int counter = 0;
var walker = Head;
while (walker != null && walker.Next != null && walker.Next.Next != null && counter != position - 1)
{
walker = walker.Next;
counter++;
}
if (walker == null || walker.Next == null)
{
throw new NullReferenceException();
}
var item = walker.Next.Value;
walker.Next = walker.Next.Next;
--Head.SizeList;
return item;
}

/// <summary>
/// Prints list items
/// </summary>
virtual public void PrintTheElements()
{
ListElement? walker = Head;
int counter = 0;
while (walker != null)
{
Console.Write(counter);
Console.Write(' ');
Console.WriteLine(walker.Value);
walker = walker.Next;
++counter;
}
}

/// <summary>
/// Changes values by position
/// </summary>
/// <exception cref="NullPointerException">Throws an exception when the list is empty</exception>
virtual public void ChangeValueByPosition(int position, int newValue)
{
if (Head == null || Tail == null)
{
throw new NullListException();
}
ListElement? walker = Head;
int counter = 0;
while (walker != null && counter != position)
{
walker = walker.Next;
counter++;
}
if (walker == null)
{
throw new NullReferenceException();
}
walker.Value = newValue;
}

/// <summary>
/// Checks if the list is empty
/// </summary>
virtual public bool IsEmpty() => Head == null;

/// <summary>
/// Returns the value by position
/// </summary>
/// <exception cref="NullPointerException">Throws an exception when the list is empty</exception>
/// <exception cref="InvalidPositionException">Throws an excwption when there is no such position in the list</exception>
virtual public int ReturnValueByPosition(int position)
{
if (Head == null || Tail == null)
{
throw new NullListException();
}
var walker = Head;
int counter = 0;
while (walker != null && counter != position)
{
walker = walker.Next;
++counter;
}
if (walker == null)
{
throw new InvalidPositionException();
}
return walker.Value;
}

/// <summary>
/// Checks if there is an item in the list
/// </summary>
/// <returns>Returns true if present and false if absent</returns>
public bool Contains(int value)
{
var walker = Head;
while (walker != null)
{
if (walker.Value == value)
{
return true;
}
walker = walker.Next;
}
return false;
}

public class ListElement
{
public ListElement(int value)
{
Value = value;
}
public int Value { get; set; }

public ListElement? Next { get; set; }

public int SizeList { get; set; }
}
}
10 changes: 10 additions & 0 deletions ListAndUniqueList/ListAndUniqueList/ListAndUniqueList.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
6 changes: 6 additions & 0 deletions ListAndUniqueList/ListAndUniqueList/NullPointerException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ListAndUniqueList;

/// <summary>
/// Throw exception when list is empty
/// </summary>
public class NullListException : Exception {}
31 changes: 31 additions & 0 deletions ListAndUniqueList/ListAndUniqueList/UniqueList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace ListAndUniqueList;

/// <summary>
/// List only without repetitions
/// </summary>
public class UniqueList : List
{
public override void AddElement(int position, int value)
{
if (!Contains(value))
{
base.AddElement(position, value);
}
else
{
throw new InvalidItemException();
}
}

public override void ChangeValueByPosition(int position, int newValue)
{
if (!Contains(newValue) || ReturnValueByPosition(position) == newValue)
{
base.ChangeValueByPosition(position, newValue);
}
else
{
throw new InvalidItemException();
}
}
}
Loading