Заключение
В процессе выполнения практической работы были освоены навыки работы с динамическим списком, реализованы методы. А также, в соответствии со своим вариантом, дополнительно реализованы еще два метода на языке программирования C#.
Приложение а
(Класс List)
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;
}
else
{
Node current = head;
while (current.Next != null)
{
current = current.Next;
}
current.Next = newNode;
}
return true;
}
public bool RemoveItem(int element)
{
if (head == null)
{
return false;
}
if (head.Data == element)
{
head = head.Next;
return true;
}
Node current = head;
while (current.Next != null)
{
if (current.Next.Data == element)
{
current.Next = current.Next.Next;
return true;
}
current = current.Next;
}
return false;
}
public bool Clear()
{
if (head == null)
{
return false;
}
head = null;
return true;
}
public bool Search(int element)
{
if (head == null)
{
return false;
}
Node current = head;
while (current != null)
{
if (current.Data == element)
{
return true;
}
current = current.Next;
}
return false;
}
public void MergeLists(List list1, List list2)
{
if (list1.head == null)
{
head = list2.head;
return;
}
if (list2.head == null)
{
head = list1.head;
return;
}
Node current1 = list1.head;
Node current2 = list2.head;
if (current1.Data <= current2.Data)
{
head = current1;
current1 = current1.Next;
}
else
{
head = current2;
current2 = current2.Next;
}
Node current = head;
while (current1 != null && current2 != null)
{
if (current1.Data <= current2.Data)
{
current.Next = current1;
current1 = current1.Next;
}
else
{
current.Next = current2;
current2 = current2.Next;
}
current = current.Next;
}
if (current1 != null)
{
current.Next = current1;
}
else
{
current.Next = current2;
}
}
public void Sort()
{
if (head == null || head.Next == null)
{
return;
}
Node current = head;
while (current != null)
{
Node minNode = current;
Node temp = new Node(0);
while (current.Next != null)
{
if (current.Next.Data < minNode.Data)
{
minNode = current.Next;
temp = current;
}
current = current.Next;
}
if (minNode != current)
{
temp.Next = minNode.Next;
minNode.Next = current.Next;
current.Next = minNode;
}
current = current.Next;
}
}
public void Display()
{
Node current = head;
while (current != null)
{
Console.Write(current.Data + " ");
current = current.Next;
}
Console.WriteLine();
}
}
}