-
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 all 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,204 @@ | ||
| 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> | ||
| public class SinglyLinkedList<T> where T : IComparable<T> | ||
| { | ||
| /// <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; | ||
|
|
||
| /// <summary> | ||
| /// List size | ||
| /// </summary> | ||
| public int Size { get; private set; } | ||
|
|
||
| /// <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(); | ||
| 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; | ||
| ListElement? copyElement = null; | ||
|
|
||
| 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 virtual 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> | ||
| /// 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.CompareTo(element.Value) == 0) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| element = element.Next; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Function for clear list | ||
| /// </summary> | ||
| public void ClearList() => head = null; | ||
|
|
||
| public T GetItemByIndex(int index) | ||
| { | ||
| if (index >= Size || index < 0) | ||
| { | ||
| throw new ArgumentOutOfRangeException(); | ||
| } | ||
|
|
||
| ListElement? element = head; | ||
| for (int i = 0; i < index; i++) | ||
| { | ||
| element = element?.Next; | ||
| } | ||
|
|
||
| return element!.Value!; | ||
| } | ||
|
|
||
| } | ||
| 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,6 @@ | ||
| namespace List; | ||
|
|
||
| /// <summary> | ||
| /// Exception for the case of deleting a non-existent element | ||
| /// </summary> | ||
| public class RemoveNonExistingElementException : InvalidOperationException { } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace List; | ||
|
|
||
| /// <summary> | ||
| /// Exception for the case of adding an existing element | ||
| /// </summary> | ||
| public class RepeatValueException : InvalidOperationException { } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| namespace List; | ||
|
|
||
| /// <summary> | ||
| /// Class representing a list of unique values | ||
| /// </summary> | ||
| public class UniqueList<T> : SinglyLinkedList<T> where T : IComparable<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; | ||
| } | ||
|
|
||
| /// <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 override bool ChangeElement(int index, T value) | ||
| { | ||
| if (index >= Size || index < 0) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (base.GetItemByIndex(index).CompareTo(value) == 0) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (Contains(value)) | ||
| { | ||
| throw new InvalidOperationException(); | ||
| } | ||
|
|
||
| base.ChangeElement(index, value); | ||
| return true; | ||
| } | ||
| } | ||
|
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.
По-хорошему public-свойствам тоже комментарии нужны