-
Notifications
You must be signed in to change notification settings - Fork 0
Queue #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: master
Are you sure you want to change the base?
Queue #10
Changes from all commits
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,58 @@ | ||
| using NUnit.Framework; | ||
|
|
||
|
|
||
| namespace test2.Test | ||
| { | ||
| public class Tests | ||
| { | ||
| private Queue queue; | ||
|
|
||
| [SetUp] | ||
| public void Setup() | ||
| { | ||
| queue = new Queue(); | ||
| queue.Enqueue(1, 1); | ||
| queue.Enqueue(9, 9); | ||
| queue.Enqueue(2, 2); | ||
| queue.Enqueue(5, 5); | ||
| queue.Enqueue(3, 3); | ||
| queue.Enqueue(4, 4); | ||
| queue.Enqueue(5, 10); | ||
| queue.Enqueue(7, 10); | ||
| queue.Enqueue(10, 10); | ||
| queue.Enqueue(7, 7); | ||
| queue.Enqueue(8, 8); | ||
| queue.Enqueue(6, 6); | ||
| } | ||
|
|
||
| [Test] | ||
| public void EnqueueTest() | ||
| { | ||
| var queueNew = new Queue(); | ||
| Assert.IsTrue(queueNew.Empty()); | ||
| queueNew.Enqueue(1, 4); | ||
| Assert.IsFalse(queueNew.Empty()); | ||
| } | ||
|
|
||
| [Test] | ||
| public void DequeueTest() | ||
| { | ||
| int[] result1 = new int[4]{ 5, 7, 10, 9 }; | ||
| var functionResult = queue.Dequeue(); | ||
| Assert.AreEqual(result1[0], functionResult); | ||
| functionResult = queue.Dequeue(); | ||
| Assert.AreEqual(result1[1], functionResult); | ||
| functionResult = queue.Dequeue(); | ||
| Assert.AreEqual(result1[2], functionResult); | ||
| functionResult = queue.Dequeue(); | ||
| Assert.AreEqual(result1[3], functionResult); | ||
| } | ||
|
|
||
| [Test] | ||
| public void DequeueExceptionTest() | ||
| { | ||
| var queueException = new Queue(); | ||
| Assert.Throws<QueueException>(() => queueException.Dequeue()); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net5.0</TargetFramework> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="NUnit" Version="3.12.0" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="3.16.1" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\test2\test2.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| 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 16 | ||
| VisualStudioVersion = 16.0.31205.134 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test2", "test2\test2.csproj", "{020B075D-FF1A-41BD-90FF-1A545F520156}" | ||
| EndProject | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test2.Test", "test2.Test\test2.Test.csproj", "{65AEFBC3-3090-41B6-B4C3-F17B836DC26B}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {020B075D-FF1A-41BD-90FF-1A545F520156}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {020B075D-FF1A-41BD-90FF-1A545F520156}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {020B075D-FF1A-41BD-90FF-1A545F520156}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {020B075D-FF1A-41BD-90FF-1A545F520156}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {65AEFBC3-3090-41B6-B4C3-F17B836DC26B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {65AEFBC3-3090-41B6-B4C3-F17B836DC26B}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {65AEFBC3-3090-41B6-B4C3-F17B836DC26B}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {65AEFBC3-3090-41B6-B4C3-F17B836DC26B}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {29C9CA88-0E80-4B80-A756-321F9F4FBCC9} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| using System; | ||
|
|
||
| namespace test2 | ||
|
Collaborator
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. Пространства имён в .NET именуются с заглавной |
||
| { | ||
| class Program | ||
| { | ||
| static void Main() | ||
| { | ||
| var queue = new Queue(); | ||
| queue.Enqueue(1, 1); | ||
| queue.Enqueue(9, 9); | ||
| queue.Enqueue(2, 2); | ||
| queue.Enqueue(5, 5); | ||
| queue.Enqueue(3, 3); | ||
| queue.Enqueue(4, 4); | ||
| queue.Enqueue(5, 10); | ||
| queue.Enqueue(7, 10); | ||
| queue.Enqueue(10, 10); | ||
| queue.Enqueue(7, 7); | ||
| queue.Enqueue(8, 8); | ||
| queue.Enqueue(6, 6); | ||
| var result = queue.Dequeue(); | ||
| Console.WriteLine(result); | ||
| result = queue.Dequeue(); | ||
| Console.WriteLine(result); | ||
| result = queue.Dequeue(); | ||
| Console.WriteLine(result); | ||
| result = queue.Dequeue(); | ||
| Console.WriteLine(result); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| using System; | ||
|
|
||
| namespace test2 | ||
| { | ||
| /// <summary> | ||
| /// queue with priority | ||
| /// </summary> | ||
| public class Queue | ||
| { | ||
| /// <summary> | ||
| /// node on queue | ||
| /// </summary> | ||
| private class Node | ||
| { | ||
| public int Priority { get; set; } | ||
|
|
||
| public int Value { get; set; } | ||
|
|
||
| public Node Next { get; set; } | ||
|
Comment on lines
+15
to
+19
Collaborator
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-сеттеры? Ведь если значение попадает в очередь, оно уже не меняется, да и приоритет его тоже. |
||
| public Node(int priority, int value) | ||
| { | ||
| Value = value; | ||
| Priority = priority; | ||
| Next = null; | ||
| } | ||
| } | ||
|
|
||
| private Node head; | ||
|
|
||
| private Node tail; | ||
|
|
||
| public Queue() | ||
| => head = null; | ||
|
Collaborator
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. Он и так null, тем более что tail Вы не инициализируете :) |
||
|
|
||
|
|
||
| /// <summary> | ||
| /// add to queue | ||
| /// </summary> | ||
| public void Enqueue(int value, int priority) | ||
| { | ||
| if (head == null) | ||
| { | ||
| head = new Node(priority, value); | ||
| head.Next = null; | ||
| tail = head; | ||
| return; | ||
| } | ||
| var node = tail; | ||
| var newNode = new Node(priority, value); | ||
| tail = newNode; | ||
| node.Next = tail; | ||
| } | ||
|
|
||
| private Node runner; | ||
|
Collaborator
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. Лучше все поля и все методы объявлять рядом, не перемешивая, тем более что конкретно у этой штуки нет ни одной причины быть полем (она всегда инициализируется в методе перед использованием, следовательно состояние объекта на самом деле не хранит, следовательно могла бы быть локальной переменной). |
||
|
|
||
| private int GetMaxPriority() | ||
| { | ||
| int priority = int.MinValue; | ||
| runner = head; | ||
| while (runner != null) | ||
| { | ||
| if (runner.Priority > priority) | ||
| { | ||
| priority = runner.Priority; | ||
| runner = runner.Next; | ||
| continue; | ||
| } | ||
| runner = runner.Next; | ||
| } | ||
| return priority; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// function remove from queue | ||
| /// </summary> | ||
| /// <returns>value's array with max priority</returns> | ||
|
Collaborator
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 int Dequeue() | ||
| { | ||
| if (Empty()) | ||
| { | ||
| throw new QueueException("Queue is Empty!"); | ||
| } | ||
| int maxPriority = GetMaxPriority(); | ||
| runner = head; | ||
| int result = 0; | ||
| Node previosNode = null; | ||
| while (runner.Next != null) | ||
| { | ||
| if (runner.Next.Priority == maxPriority) | ||
|
Collaborator
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. Гм. Если самый большой приоритет прячется в голове очереди, что будет? Мы же его пропустим тут? |
||
| { | ||
| result = runner.Next.Value;; | ||
| runner.Next = runner.Next.Next; | ||
| previosNode = runner; | ||
| break; | ||
| } | ||
| runner = runner.Next; | ||
| } | ||
| if (runner.Priority == maxPriority && runner.Next == null) | ||
| { | ||
| result = runner.Next.Value; | ||
|
Collaborator
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. runner.Next == null же, иначе мы бы сюда не попали. runner.Next.Value --- это NullReferenceException |
||
| runner = null; | ||
| tail = previosNode; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// is queue empty? | ||
| /// </summary> | ||
| /// <returns>true - queue is empty</returns> | ||
| public bool Empty() | ||
| => head == null; | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace test2 | ||
| { | ||
| public class QueueException : Exception | ||
|
Collaborator
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 QueueException() | ||
| { | ||
| } | ||
|
|
||
| public QueueException(string message) | ||
| : base(message) | ||
| { | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net5.0</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
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.
Честно говоря, не очень понятно, зачем-таки result. Можно было просто проверить на равенство 5, 7 и т.д.