-
Notifications
You must be signed in to change notification settings - Fork 0
List #10
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
base: main
Are you sure you want to change the base?
List #10
Changes from 5 commits
1276141
9ddbf64
ab28fb7
76814e6
b441c5f
fb1242c
6dfba1d
5912954
606580e
fe39871
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| 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;} | ||
| } | ||
| 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> | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; } | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(){}; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| 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(); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| /// </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; | ||||||
|
|
||||||
| } | ||||||
| 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> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace List; | ||
|
|
||
| /// <summary> | ||
| /// A class for creating custom exceptions | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Комментарий тут и ниже неправдив, надо поправить |
||
| /// </summary> | ||
| public class RemoveNonExistingElementException : Exception | ||
| { | ||
| public RemoveNonExistingElementException() : base() { } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,9 @@ | ||
| namespace List; | ||
|
|
||
| /// <summary> | ||
| /// A class for creating custom exceptions | ||
| /// </summary> | ||
| public class RepeatValueException : Exception | ||
| { | ||
| public RepeatValueException() : base() { } | ||
| } |
| 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; | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Всё-таки можно нарушить инвариант уникальности в списке. Подумайте, как |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.