Практики(Вариант №1) / Практическая работа №3
.pdfЗаключение
В ходе работы был создан односвязный динамический список, который включает реализацию базовых операций — инициализацию пустого списка, добавление элементов в конец, удаление заданного элемента, очистку списка и поиск элемента по образцу. Дополнительно реализованы функции для поиска элемента по индексу и слияния двух списков, где второй список вставляется после элемента с указанным индексом первого списка без использования третьего списка.
11
Приложение А
(обязательное) Код программы
using System; |
||
public class Node |
||
{ |
public int Value; |
|
|
||
|
public Node Next; |
|
|
public Node(int value) |
|
|
{ |
Value = value; |
|
|
|
|
} |
Next = null; |
} |
|
|
|
|
|
public class MyList |
||
{private Node head; private int count; public MyList()
{head = null; count = 0;
} |
|
|
|
public bool Add(int element) |
|||
{ |
Node newNode = new Node(element); |
||
|
|||
|
if (head == null) |
||
|
{ |
head = newNode; |
|
|
} |
||
|
|
|
|
|
else |
|
|
|
{ |
Node current = head; |
|
|
|
||
|
|
while (current.Next != null) |
|
|
|
{ |
current = current.Next; |
|
|
} |
|
|
|
|
|
|
} |
current.Next = newNode; |
|
|
|
|
|
|
count++; |
|
|
} |
return true; |
||
|
|
|
|
public bool RemoveItem(int element) |
|||
{if (head == null) return false; if (head.Value == element)
{head = head.Next; count--;
return true;
}
Node current = head;
while (current.Next != null)
{if (current.Next.Value == element)
{ current.Next = current.Next.Next;
12
|
|
count--; |
|
} |
return true; |
|
|
|
} |
current = current.Next; |
|
|
|
|
} return false; public bool Clear()
{head = null; count = 0; return true;
} |
|
|
public int Search(int element) |
||
{ |
Node current = head; |
|
|
||
|
int index = 0; |
|
|
while (current != null) |
|
|
{ |
if (current.Value == element) |
|
|
|
|
|
return index; |
|
|
current = current.Next; |
|
} |
index++; |
|
|
|
} |
return -1; |
|
|
|
|
public int GetElementByIndex(int index) |
||
{ |
if (index < 0 || index >= count) |
|
|
||
|
|
throw new IndexOutOfRangeException("Индекс находится вне диапазона списка"); |
|
Node current = head; |
|
|
for (int i = 0; i < index; i++) |
|
|
{ |
current = current.Next; |
|
} |
|
|
|
|
} |
return current.Value; |
|
|
|
|
public void MergeLists(MyList secondList, int index) |
||
{ |
if (index < 0 || index >= count) |
|
|
||
|
|
throw new IndexOutOfRangeException("Индекс находится вне диапазона списка"); |
|
if (secondList == null || secondList.head == null) |
|
|
|
return; |
|
Node current = head; |
|
|
for (int i = 0; i < index; i++) |
|
|
{ |
current = current.Next; |
|
} |
|
|
|
|
|
Node temp = current.Next; |
|
|
current.Next = secondList.head; |
|
|
Node lastOfSecond = secondList.head; |
|
|
while (lastOfSecond.Next != null) |
|
|
{ |
lastOfSecond = lastOfSecond.Next; |
|
} |
|
|
|
|
|
lastOfSecond.Next = temp; |
|
13
} |
|
|
public void Print() |
||
{ |
Node current = head; |
|
|
||
|
Console.Write("MyList: "); |
|
|
while (current != null) |
|
|
{ |
Console.Write(current.Value + " "); |
|
|
|
|
|
current = current.Next; |
|
} |
|
} |
Console.WriteLine(); |
|
|
|
|
public int Size |
||
{ get { return count; }
}}
class Program
{static void Main()
{MyList list1 = new MyList(); MyList list2 = new MyList();
list1.Add(5);
list1.Add(2);
list1.Add(8);
list1.Add(1);
list2.Add(3);
list2.Add(6);
list2.Add(4);
Console.WriteLine("Исходные списки:"); list1.Print();
list2.Print();
Console.WriteLine("\nПоиск элемента 8 в list1: " + list1.Search(8)); Console.WriteLine("Поиск элемента 10 в list1: " + list1.Search(10)); Console.WriteLine("\nЭлемент с индексом 2 в list1: " + list1.GetElementByIndex(2));
Console.WriteLine("\nУдаление элемента 2 из list1: " + list1.RemoveItem(2)); Console.WriteLine("Список после удаления:");
list1.Print();
Console.WriteLine("\nСлияние list2 в list1 после элемента с индексом 1:"); list1.MergeLists(list2, 1);
list1.Print();
Console.WriteLine("\nРазмер list1 после слияния: " + list1.Size); Console.WriteLine("Размер list2 после слияния: " + list2.Size);
Console.WriteLine("\nОчистка списка list1: " + list1.Clear()); } } Console.WriteLine("Размер после очистки: " + list1.Size);
14
