Skip to content
Open

List #10

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
21 changes: 19 additions & 2 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Build

on: [push, pull_request]
on: [push]

jobs:
build:
build-Windows:

runs-on: windows-latest

Expand All @@ -20,4 +20,21 @@ jobs:
- name: Test
run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {dotnet test $file.FullName}

build-Ubuntu:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Build
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.x'
- name: Restore
run: for f in $(find . -name "*.sln"); do dotnet restore $f; done
- name: Build
run: for f in $(find . -name "*.sln"); do dotnet build $f; done
- name: Test
run: for f in $(find . -name "*.sln"); do dotnet test $f; done


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
204 changes: 204 additions & 0 deletions List/List/List/List/List.cs
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; }

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();
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;

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 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!;
}

}
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>
6 changes: 6 additions & 0 deletions List/List/List/List/RemoveNonExistingElementException.cs
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 { }
6 changes: 6 additions & 0 deletions List/List/List/List/RepeatValueException.cs
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 { }
76 changes: 76 additions & 0 deletions List/List/List/List/UniqueList.cs
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;
}
}

Choose a reason for hiding this comment

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

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

Loading