
СД Практика 3
.pdfreturn null;
}
// Слияние двух списков
public void Merge(List otherList, int index)
{
Node current = head; int count = 0;
while (current != null && count < index)
{
current = current.Next; count++;
}
if (current == null) return;
if (otherList.head != null)
{
otherList.head.Prev = current; otherList.tail.Next = current.Next;
if (current.Next != null)
{
current.Next.Prev = otherList.tail;
}
else
{
tail = otherList.tail;
21
}
current.Next = otherList.head;
}
}
// Вывод списка
public void PrintList()
{
Node current = head; while (current != null)
{
Console.Write(current.Data + " "); current = current.Next;
}
Console.WriteLine();
}
}
public class Program
{
public static void Main()
{
try
{
List myList = new List();
Console.WriteLine("Введите элементы первого списка через пробел: "); string spisok1 = Console.ReadLine();
int[] a = spisok1.Split(' ').Select(x => int.Parse(x)).ToArray(); for (int i = 0; i < a.Length; i++)
22
{
myList.Add(a[i]);
}
Console.WriteLine("Список:"); myList.PrintList(); Console.WriteLine();
Console.WriteLine("Введите элемент, который хотите добавить: "); int add = int.Parse(Console.ReadLine());
myList.Add(add);
Console.WriteLine("Список после добавления элемента: "); myList.PrintList();
Console.WriteLine();
Console.WriteLine("Введите элемент, который хотите удалить: "); int delete = int.Parse(Console.ReadLine()); myList.RemoveItem(delete);
Console.WriteLine("Список после удаления элемента:"); myList.PrintList();
Console.WriteLine();
Console.WriteLine("Введите элемент, индекс которого нужно найти"); int poisk1 = int.Parse(Console.ReadLine());
var foundValue1 = myList.Search(poisk1); Console.WriteLine($"Индекс элемента: {foundValue1}"); Console.WriteLine();
Console.WriteLine("Введите индекс элемента, который нужно найти"); int poisk2 = int.Parse(Console.ReadLine());
23
var foundValue2 = myList.SearchByIndex(poisk2);
Console.WriteLine("Элемент по индексу: " + (foundValue2.HasValue ? foundValue2.Value.ToString() : "Нет элемента"));
Console.WriteLine();
List anotherList = new List();
Console.WriteLine("Введите элементы второго списка через пробел: "); string spisok2 = Console.ReadLine();
Console.WriteLine();
int[] b = spisok2.Split(' ').Select(x => int.Parse(x)).ToArray(); for (int i = 0; i < b.Length; i++)
{
anotherList.Add(b[i]);
}
Console.WriteLine("Второй список:"); anotherList.PrintList(); Console.WriteLine();
Console.WriteLine("Введите индекс элемента, после которого хотите присоединить список");
int poisk3 = int.Parse(Console.ReadLine()); var foundValue3 = myList.SearchByIndex(poisk3);
Console.WriteLine("Элемент по индексу: " + (foundValue3.HasValue ? foundValue3.Value.ToString() : "Нет элемента"));
Console.WriteLine();
myList.Merge(anotherList, Convert.ToInt32(foundValue3 - 1)); Console.WriteLine("Список после слияния:"); myList.PrintList();
24
Console.WriteLine();
myList.Clear();
Console.WriteLine("Список очищен");
myList.PrintList();
}
catch (Exception)
{
Console.WriteLine("Что-то пошло не так :(");
}
}
}
25