Skip to content
Open

List #10

Show file tree
Hide file tree
Changes from 5 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
31 changes: 31 additions & 0 deletions List/List/List/List.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32228.430
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "List", "List\List.csproj", "{E452EFCC-D3CF-4F34-86B6-FFF9119434ED}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ListTest", "ListTest\ListTest.csproj", "{D91A340C-44C7-462D-8304-FF4C9E6170FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E452EFCC-D3CF-4F34-86B6-FFF9119434ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E452EFCC-D3CF-4F34-86B6-FFF9119434ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E452EFCC-D3CF-4F34-86B6-FFF9119434ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E452EFCC-D3CF-4F34-86B6-FFF9119434ED}.Release|Any CPU.Build.0 = Release|Any CPU
{D91A340C-44C7-462D-8304-FF4C9E6170FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D91A340C-44C7-462D-8304-FF4C9E6170FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D91A340C-44C7-462D-8304-FF4C9E6170FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D91A340C-44C7-462D-8304-FF4C9E6170FE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8A75F5F0-0627-4DE9-BE2B-FBB251E5CA4F}
EndGlobalSection
EndGlobal
57 changes: 57 additions & 0 deletions List/List/List/List/IUniqueList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace List;

/// <summary>
/// Class interface for a unique list
/// </summary>
/// <typeparam name="T">Еype of items in the list</typeparam>
public interface IUniqueList<T>
{
/// <summary>
/// Function for changing the value of an element by index
/// </summary>
/// <param name="index">Item position in the list</param>
/// <param name="value">New value</param>
public bool ChangeElement(int index, T value);

/// <summary>
/// Function for printing a list
/// </summary>
public void PrintList();

/// <summary>
/// Аunction to search for an item in the list
/// </summary>
/// <param name="value">Search value</param>
/// <returns>Is there an item in the list</returns>
public bool Contains(T value);

/// <summary>
/// Function for deleting a list
/// </summary>
public void ClearList();

/// <summary>
/// Function for adding an item to a list
/// </summary>
/// <param name="value">Type of item value in the list</param>
public void Add(T value);

/// <summary>
/// Function to remove an item from the unique list
/// </summary>
/// <param name="index">Item position in the list</param>
public bool RemoveAt(int index);

/// <summary>
/// Function to remove an item from the list
/// </summary>
/// <param name="value">Value to be deleted</param>
/// <returns>was the value in the list</returns>
public bool Remove(T value);

/// <summary>
/// Size property
/// </summary>
/// <returns>Number of element on list</returns>
public int Size { get;}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
public int Size { get;}
public int Size { get; }

}
183 changes: 183 additions & 0 deletions List/List/List/List/List.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
namespace List;

using System;
using System.Collections.Generic;

/// <summary>
/// Сlass representing a list
/// </summary>
/// <typeparam name="T"> type of item values in the list </typeparam>
abstract public class SinglyLinkedList<T> : IUniqueList<T>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Обычный список не обязан содержать уникальные элементы. Либо у Вас интерфейс неудачно назван, либо иерархия наследования сломана

{
/// <summary>
/// Class for storing list items
/// </summary>
private class ListElement
{
public T? Value { get ; set; }
public ListElement? Next { get; set; }
}

private ListElement? head;
private ListElement? tail;
public int Size { get; private set; }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

По-хорошему public-свойствам тоже комментарии нужны


/// <summary>
/// Function for adding an item to a list
/// </summary>
/// <param name="value">T ype of item value in the list</param>
public virtual void Add(T value)
{
if (value == null)
{
return;
}

Size++;
if (head == null || tail == null)
{
head = new ListElement();
tail = head;
head.Value = value;
return;
}

var newTail = new ListElement(){};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
var newTail = new ListElement(){};
var newTail = new ListElement();

newTail.Value = value;
tail.Next = newTail;
tail = newTail;
}

/// <summary>
/// Function to remove an item from the list
/// </summary>
/// <param name="index">Item position in the list</param>
/// <returns>was the item in the list</returns>
public virtual bool RemoveAt(int index)
{
if (index >= Size || index < 0)
{
return false;
}

Size--;
if (index == 0)
{
head = head?.Next;
return true;
}

var element = head;
var copyElement = new ListElement();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Для удаления элемента как бы не надо выделять память на куче

for (int i = 0; i < index - 1; i++)
{
if (i == index - 1)
{
copyElement = element;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Тем более что тут мы её забываем. Утечка памяти. С которой, конечно, разберётся сборщик мусора, но сборщик мусора работает очень небесплатно в плане скорости работы

}

element = element?.Next;
}

if (element != null && copyElement != null)
{
copyElement.Next = element.Next;
element.Next = null;
}

return true;
}

/// <summary>
/// Function to remove an item from the list
/// </summary>
/// <param name="value">Value to be deleted</param>
/// <returns>was the value in the list</returns>
public virtual bool Remove(T value)
{
var copyHead = head;
for (int i = 0; i < Size; i++)
{
while (copyHead!= null)
{
if (copyHead != null && value != null && value.Equals(copyHead.Value))
{
RemoveAt(i);
return true;
}

copyHead = copyHead?.Next;
}
}

return false;
}

/// <summary>
/// Function for changing the value of an element by index
/// </summary>
/// <param name="index">Item position in the list</param>
/// <param name="value">New value</param>
public bool ChangeElement(int index, T value)
{
if (index >= Size || index < 0)
{
return false;
}

var element = head;
for (int i = 0; i < index - 1; i++)
{
element = element?.Next;
}

if (element != null)
{
element.Value = value;
}

return true;
}

/// <summary>
/// Function for printing a list
/// </summary>
public void PrintList()
{
var element = head;
while (element != null)
{
Console.Write($"{element.Value} ");
element = element.Next;
}

Console.WriteLine();
}

/// <summary>
/// Аunction to search for an item in the list
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
/// Аunction to search for an item in the list
/// Function to search for an item in the list

/// </summary>
/// <param name="value">Search value</param>
/// <returns>Is there an item in the list</returns>
public bool Contains(T value)
{
var element = head;
while (element != null)
{
if (value != null && value.Equals(element.Value))
{
return true;
}

element = element.Next;
}

return false;
}

/// <summary>
/// Function for clear list
/// </summary>
public void ClearList() => head = null;

}
10 changes: 10 additions & 0 deletions List/List/List/List/List.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>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

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

/// <summary>
/// A class for creating custom exceptions
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Комментарий тут и ниже неправдив, надо поправить

/// </summary>
public class RemoveNonExistingElementException : Exception
{
public RemoveNonExistingElementException() : base() { }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

А пустой конструктор без параметров не нужен, он сам сгенерится

}
9 changes: 9 additions & 0 deletions List/List/List/List/RepeatValueException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace List;

/// <summary>
/// A class for creating custom exceptions
/// </summary>
public class RepeatValueException : Exception
{
public RepeatValueException() : base() { }
}
50 changes: 50 additions & 0 deletions List/List/List/List/UniqueList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace List;

/// <summary>
/// Class representing a list of unique values
/// </summary>
public class UniqueList<T> : SinglyLinkedList<T>
{
/// <summary>
/// Function for adding an item to a list
/// </summary>
/// <param name="value">Type of item value in the list</param>
public override void Add(T value)
{
if (Contains(value))
{
throw new RepeatValueException();
}

base.Add(value);
}

/// <summary>
/// Function to remove an item from the unique list
/// </summary>
/// <param name="index">Item position in the list</param>
public override bool RemoveAt(int index)
{
if (!base.RemoveAt(index))
{
throw new RemoveNonExistingElementException();
}

return true;
}

/// <summary>
/// Function to remove an item from the list
/// </summary>
/// <param name="value">Value to be deleted</param>
/// <returns>was the value in the list</returns>
public override bool Remove(T value)
{
if (!base.Remove(value))
{
throw new RemoveNonExistingElementException();
}

return false;
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Всё-таки можно нарушить инвариант уникальности в списке. Подумайте, как

Loading