Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Практическая работа №3

.pdf
Скачиваний:
0
Добавлен:
24.01.2025
Размер:
820.92 Кб
Скачать

Заключение

В ходе выполнения практической работы была написана программа,

реализующая динамический список при помощи двух классов. Цель лабораторной работы была достигнута, так как программа выполняла все поставленные перед ней задачи. Отчет был написан согласно ОС ТУСУР 012021.

11

Приложение А

Код программы

static void FillList(List list)

{

Console.WriteLine("Введите числа для добавления в список (вводите 'exit' для завершения):");

while (true)

{

string input = Console.ReadLine(); if (input.ToLower() == "exit")

{

break;

}

if (int.TryParse(input, out int number))

{

list.Add(number);

}

else

{

Console.WriteLine("Пожалуйста, введите корректное число.");

}

}

}

int nn, mm; Console.WriteLine("Введенные числа"); List list1 = new List(); FillList(list1);

Console.WriteLine("Изначальный вид списка"); list1.PrintList();

Console.WriteLine("Какое число удалить?"); string n = Console.ReadLine();

nn = int.Parse(n); list1.RemoveItem(nn); list1.PrintList();

Console.WriteLine("Какое число проверить?"); string m = Console.ReadLine();

mm = int.Parse(m); Console.WriteLine(list1.Find(mm));

Console.WriteLine("Удалить все повторяющиеся элементы"); list1.RemoveDuplicates();

list1.PrintList();

Console.WriteLine("Удалить все четные элементы"); list1.RemoveEven();

list1.PrintList(); Console.WriteLine("Удалить все элементы"); list1.Clear();

list1.PrintList();

public class Node

{

public int Value { get; set; } public Node Next { get; set; } public Node(int value)

{

Value = value; Next = null;

}

}

12

public class List

{

private Node head; public List()

{ head = null; }

public bool Add(int element)

{

Node newNode = new Node(element); if (head == null)

{

head = newNode; head.Next = head;

}

else

{

Node current = head;

while (current.Next != head)

{

current = current.Next;

}

current.Next = newNode; newNode.Next = head;

}

return true;

}

public bool RemoveItem(int element)

{

if (head == null) return false; Node current = head;

Node prev = null;

if (head.Value == element)

{

if (head.Next == head)

{

head = null;

}

else

{

Node last = head;

while (last.Next != head) { last = last.Next; } head = head.Next; last.Next = head;

}

return true;

}

do

{

prev = current;

current = current.Next;

if (current.Value == element)

{

prev.Next = current.Next; return true;

}

} while (current != head); return false;

}

public bool Clear()

{

if (head == null) return false head = null;

return true;

}

13

public int Find(int element)

{

if (head == null) return -1; int index = 0;

Node current = head; do

{

if (current.Value == element) return index; index++;

current = current.Next; } while (current != head); return -1;

}

public bool RemoveEven()

{

if (head == null) return false; Node current = head;

Node prev = null

bool removed = false; do

{

if (current.Value % 2 == 0)

{

removed = true;

if (current == head)

{

RemoveItem(current.Value); current = head;

}

else

{

prev.Next = current.Next current = prev.Next;

}

}

else

{

prev = current

current = current.Next;

}

} while (current != head); return removed;

}

public bool RemoveDuplicates()

{

if (head == null) return false; Node current = head;

bool removed = false; do

{

Node runner = current; while (runner.Next != head)

{

if (runner.Next.Value == current.Value)

{

runner.Next = runner.Next.Next; removed = true;

}

else

{

runner = runner.Next;

}

}

current = current.Next; } while (current != head);

14

return removed;

}

public void PrintList()

{

if (head == null)

{

Console.WriteLine("(Список пуст)"); return;

}

Node current = head; do

{

Console.Write(current.Value + " "); current = current.Next;

} while (current != head); Console.WriteLine();

}

}

15