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

Практики / ПР4 / СД_П4

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

{

head = null; tail = null;

}

public bool Add(int element)

{

Node newNode = new Node(element);

if (head == null)

{

head = newNode; tail = newNode; tail.Next = head;

}

else

{

tail.Next = newNode; tail = newNode; tail.Next = head;

}

11

return true;

}

public bool RemoveItem(int element)

{

if (head == null)

{

return false;

}

Node current = head;

Node previous = null;

do

{

if (current.Value == element)

{

if (previous == null)

{

if (head == tail)

{

head = null;

12

tail = null;

}

else

{

head = head.Next; tail.Next = head;

}

}

else

{

previous.Next = current.Next; if (current == tail)

{

tail = previous;

}

}

return true;

}

previous = current; current = current.Next;

13

} while (current != head);

return false;

}

public bool Clear()

{

if (head == null)

{

return false;

}

head = null; tail = null;

return true;

}

public bool Search(int element)

{

if (head == null)

{

14

return false;

}

Node current = head;

do

{

if (current.Value == element)

{

return true;

}

current = current.Next;

} while (current != head);

return false;

}

public void PrintList()

{

if (head == null)

15

{

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

}

Node current = head;

do

{

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

} while (current != head);

Console.WriteLine();

}

public void RemoveEven()

{

if (head == null)

{

return;

}

16

Node current = head;

Node previous = null;

do

{

if (current.Value % 2 == 0)

{

if (current == head)

{

if (head == tail)

{

head = null; tail = null;

}

else

{

head = head.Next; tail.Next = head; current = head;

}

}

17

else

{

previous.Next = current.Next; if (current == tail)

{

tail = previous;

}

current = previous.Next;

}

}

else

{

previous = current; current = current.Next;

}

} while (current != head);

}

public void RemoveDuplicates()

{

if (head == null)

18

{

return;

}

Node current = head;

do

{

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

{

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

{

if (runner.Next == tail)

{

tail = runner;

}

runner.Next = runner.Next.Next;

}

else

{

19

runner = runner.Next;

}

}

current = current.Next;

} while (current != head);

}

}

// Класс стек

class Stack : MyList

{

private int count = 0;

public bool Push(int element)

{

Add(element);

count++; return true;

}

20

Соседние файлы в папке ПР4