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

Практика 3 - Списки - СФ

.pdf
Скачиваний:
8
Добавлен:
06.11.2022
Размер:
374.15 Кб
Скачать

Приложение А

(обязательное)

Листинг программы

using System;

using System.Collections.Generic; using System.Collections;

using System.Linq;

namespace SystemOfAData3

{

public class Node<T>

{

public Node(T data)

{

Data = data;

}

public T Data { get; set; } public Node<T> Next { get; set; }

}

public class List<T> : IEnumerable<T>

{

Node<T> head; Node<T> tail; int count; public void Add(T data)

{

Node<T> node = new Node<T>(data); if (head == null)

{

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

}

else

{

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

}

count++;

}

public bool Remove(T data)

{

Node<T> current = head; Node<T> previous = null; if (IsEmpty) return false;

do

{

if (current.Data.Equals(data))

{

if (previous != null)

{

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

tail = previous;

}

else

{

if (count == 1)

{

head = tail = null;

}

else

{

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

}

}

11

count--; return true;

}

previous = current; current = current.Next;

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

}

public bool IsEmpty { get { return count == 0; } } public void Clear()

{

head = null; tail = null; count = 0;

}

public int Count { get { return count; } } public string Index(T data)

{

Node <T> index = head; int countIndex = 0;

if (index == null) return "Элемента нет!!!"; do

{

if (index.Data.Equals(data)) return $"{countIndex}";

index = index.Next; countIndex++;

}

while (index != head); return "Элемента нет!!!";

}

IEnumerator IEnumerable.GetEnumerator()

{

return ((IEnumerable)this).GetEnumerator();

}

IEnumerator<T> IEnumerable<T>.GetEnumerator()

{

Node<T> current = head; do

{

if (current != null)

{

yield return current.Data; current = current.Next;

}

}

while (current != head);

}

}

class Program

{

static void Swap(ref int p1, ref int p2)

{

int temp = p1; p1 = p2;

p2 = temp;

}

static void QuickSort(int[] arr, int first, int last)

{

int p = arr[(last - first) / 2 + first]; int i = first, j = last;

while (i <= j)

{

while (arr[i] < p && i <= last)

12

i++;

while (arr[j] > p && j >= first) j--;

if (i <= j)

{

Swap(ref arr[i], ref arr[j]); i++; j--;

}

}

if (j > first) QuickSort(arr, first, j); if (i < last) QuickSort(arr, i, last);

}

static void Main(string[] args)

{

Console.Write("Введите размер списка - "); int n = int.Parse(Console.ReadLine()); List<int> list1 = new List<int>(); List<int> list2 = new List<int>();

Random rand = new Random(); int[] array1 = new int[n]; int[] array2 = new int[n];

for (int i = 0; i < array1.Length; i++)

{

array1[i] = rand.Next(1, 500); array2[i] = rand.Next(1, 500);

}

foreach (int d in array1) list1.Add(d); foreach (int d in array2) list2.Add(d); Console.WriteLine("Первый список " + "\n");

foreach (int d in list1) Console.Write(d + "\t"); Console.WriteLine(); Console.WriteLine("========================" + "\n"); Console.WriteLine("Второй список " + "\n");

foreach (int d in list2) Console.Write(d + "\t"); Console.WriteLine();

Console.WriteLine();

Console.Write("Число для добавления - "); list1.Add(int.Parse(Console.ReadLine())); Console.WriteLine(); Console.WriteLine("Измененный список: " + "\n"); foreach (int d in list1) Console.Write(d + "\t"); Console.WriteLine();

Console.WriteLine(); Console.Write("Число для удаления - ");

list1.Remove(int.Parse(Console.ReadLine())); Console.WriteLine(); Console.WriteLine("Измененный список: " + "\n"); foreach (int d in list1) Console.Write(d + "\t"); Console.WriteLine();

Console.WriteLine();

Console.Write("Найти индекс элемента - "); Console.WriteLine($"Индекс элемента -

{list1.Index(int.Parse(Console.ReadLine()))}"); Console.ReadLine();

Union(list1, list2); Console.WriteLine("Целый список " + "\n");

foreach (int d in list2) Console.Write(d + "\t"); Console.WriteLine();

}

 

static void

Union(List<int> list1, List<int> list2)

{

 

foreach

(int d in list1) list2.Add(d);

int[] f

= new int[list2.Count];

int k =

0;

foreach

(int d in list2)

 

13

{

f[k] = d; k++;

}

list2.Clear();

QuickSort(f, 0, f.Length - 1); foreach (int d in f) list2.Add(d);

}

}

}

14