generated from admin/template
Загрузить файлы в «works/api/1»
This commit is contained in:
@@ -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})");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user