From 645ede3b0f5bc851be2b0cb9b9da60282e862e7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D1=91=D0=BD=D1=8C=D0=BA=D0=B0=20=D0=A7=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=B2=D0=BA=D0=BE?= <71+lenya228cherevko@noreply.localhost> Date: Fri, 16 Oct 2026 11:01:36 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20=C2=AB?= =?UTF-8?q?works/algo=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- works/algo/Program.cs | 288 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 288 insertions(+) create mode 100644 works/algo/Program.cs diff --git a/works/algo/Program.cs b/works/algo/Program.cs new file mode 100644 index 0000000..db8e9b9 --- /dev/null +++ b/works/algo/Program.cs @@ -0,0 +1,288 @@ +using System; +using System.Collections.Generic; + +namespace PokedexApp +{ + + public class PokemonStage + { + public string Name { get; set; } + public string Type { get; set; } + public int RequiredLevel { get; set; } + + public PokemonStage(string name, string type, int requiredLevel) + { + Name = name; + Type = type; + RequiredLevel = requiredLevel; + } + + public void ShowInfo() + { + Console.WriteLine($"{Name} (Ур. {RequiredLevel}, {Type})"); + } + } + + public class EvolutionNode + { + public PokemonStage Data { get; set; } + public EvolutionNode? Next { get; set; } + + public EvolutionNode(PokemonStage data) + { + Data = data; + Next = null; + } + } + + public class EvolutionLine + { + public EvolutionNode? Head { get; private set; } + public int Count { get; private set; } + + public EvolutionLine() + { + Head = null; + Count = 0; + } + + public void AddFirst(PokemonStage stage) + { + var node = new EvolutionNode(stage); + node.Next = Head; + Head = node; + Count++; + } + + public void AddLast(PokemonStage stage) + { + var node = new EvolutionNode(stage); + + if (Head == null) + { + Head = node; + } + else + { + var current = Head; + while (current.Next != null) + { + current = current.Next; + } + + current.Next = node; + } + + Count++; + } + + public void PrintAll() + { + var current = Head; + int index = 1; + + while (current != null) + { + Console.WriteLine( + $"{index}. {current.Data.Name} (Ур. {current.Data.RequiredLevel}, {current.Data.Type})"); + current = current.Next; + index++; + } + + Console.WriteLine($"\nВсего этапов: {Count}"); + } + + public PokemonStage? Find(string name) + { + var current = Head; + while (current != null) + { + if (current.Data.Name == name) + return current.Data; + current = current.Next; + } + + return null; + } + + public bool Contains(string name) + { + return Find(name) != null; + } + + public bool Remove(string name) + { + if (Head == null) + return false; + + if (Head.Data.Name == name) + { + Head = Head.Next; + Count--; + return true; + } + + var current = Head; + while (current.Next != null && current.Next.Data.Name != name) + { + current = current.Next; + } + + if (current.Next == null) + return false; + + current.Next = current.Next.Next; + Count--; + return true; + } + + public bool InsertAfter(string existingName, PokemonStage newStage) + { + var current = Head; + while (current != null) + { + if (current.Data.Name == existingName) + { + var node = new EvolutionNode(newStage); + node.Next = current.Next; + current.Next = node; + Count++; + return true; + } + + current = current.Next; + } + + return false; + } + + public void Reverse() + { + EvolutionNode? prev = null; + var current = Head; + + while (current != null) + { + var next = current.Next; + current.Next = prev; + prev = current; + current = next; + } + + Head = prev; + } + + public PokemonStage? FindMiddle() + { + if (Head == null) return null; + + var slow = Head; + var fast = Head; + + while (fast != null && fast.Next != null) + { + slow = slow.Next!; + fast = fast.Next.Next; + } + + return slow.Data; + } + + public bool HasCycle() + { + if (Head == null) return false; + + var slow = Head; + var fast = Head; + + while (fast != null && fast.Next != null) + { + slow = slow.Next!; + fast = fast.Next.Next; + + if (slow == fast) + return true; + } + + return false; + } + + public void CreateFakeCycleForTesting() + { + if (Head == null) return; + var current = Head; + while (current.Next != null) + { + current = current.Next; + } + + current.Next = Head; + } + } + + class Program + { + static void Main(string[] args) + { + Console.WriteLine("=== ЗАДАНИЕ 1: ЭВOЛЮЦИOННAЯ ЛИНИЯ ==="); + EvolutionLine charmanderLine = new EvolutionLine(); + + charmanderLine.AddLast(new PokemonStage("Чармандер", "огненный", 1)); + charmanderLine.AddLast(new PokemonStage("Чармелеон", "огненный", 16)); + charmanderLine.AddLast(new PokemonStage("Чаризард", "огненный/летающий", 36)); + + charmanderLine.PrintAll(); + Console.WriteLine(new string('-', 50)); + + Console.WriteLine("\n=== ЗАДАНИЕ 2: OПEРAЦИИ С ЛИНИEЙ ==="); + + Console.Write("Ищем Чармелеона... "); + var found = charmanderLine.Find("Чармелеон"); + if (found != null) + Console.WriteLine($"Найден: {found.Name} (Ур. {found.RequiredLevel})"); + else + Console.WriteLine("Не найден"); + + Console.Write("Удаляем Чармелеона... "); + bool isRemoved = charmanderLine.Remove("Чармелеон"); + Console.WriteLine(isRemoved ? "Удалён." : "Ошибка удаления."); + + Console.Write("Вставляем Мега-Чаризарда X после Чаризарда... "); + bool isInserted = + charmanderLine.InsertAfter("Чаризард", new PokemonStage("Мега-Чаризард X", "огненный/дракон", 36)); + Console.WriteLine(isInserted ? "Готово.\n" : "Ошибка вставки."); + + Console.WriteLine("Текущая линия:"); + charmanderLine.PrintAll(); + Console.WriteLine(new string('-', 50)); + + Console.WriteLine("\n=== ЗАДАНИЕ 3: АЛГОРИТМЫ (РЕВЕРС, СЕРЕДИНА, ЦИКЛ) ==="); + EvolutionLine bulbasaurLine = new EvolutionLine(); + bulbasaurLine.AddLast(new PokemonStage("Бульбазавр", "Травяной", 1)); + bulbasaurLine.AddLast(new PokemonStage("Ивизавр", "Травяной", 16)); + bulbasaurLine.AddLast(new PokemonStage("Венузавр", "Травяной/Ядовитый", 32)); + + Console.WriteLine("Исходный порядок:"); + bulbasaurLine.PrintAll(); + + Console.WriteLine("\nВызываем Reverse() [Деволюция]:"); + bulbasaurLine.Reverse(); + bulbasaurLine.PrintAll(); + + bulbasaurLine.Reverse(); + + Console.Write("\nИщем средний этап эволюции: "); + var middle = bulbasaurLine.FindMiddle(); + if (middle != null) + { + Console.WriteLine($"{middle.Name} (Ур. {middle.RequiredLevel})"); + } + } + } +} + + + + + + \ No newline at end of file