СДлб3
.pdfБлок-схема изображена на рисунке 2.1
Рисунок 2.14-Блок схема алгоритма
Блок-схема вспомогательного алгоритма изображена на рисунке 2.15
11
Рисунок 2.15-Блок схема алгоритма
12
Заключение
Во время лабораторной работы была проведена реализация динамического списка при помощи двух классов. Были получены навыки работы с динамическими списками на базе языка программирования c#.
13
Приложение А
(обязательное)
using System.Collections; using System;
using System.Collections.Generic;
namespace sd3
{
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> first; Node<T> last; int count;
public void Add(T data)
{
Node<T> node = new Node<T>(data);
// если список пуст if (first == null) if (first == null)
{
first = node; last = node; last.Next = first;
}
else
{
node.Next = first; last.Next = node; last = node;
}
count++;
}
public bool Remove(T data)
{
Node<T> current = first; Node<T> previous = null; if (IsEmpty) return false; do
{
if (current.Data.Equals(data))
{
if (previous != null)
{
previous.Next = current.Next; if (current == last)
last = previous;
}
else
{
if (count == 1)
{
first = last = null;
}
14
else
{
first = current.Next; last.Next = current.Next;
}
}
count--; return true;
}
previous = current; current = current.Next;
} while (current != first);
return false;
}
public bool IsEmpty { get { return count == 0; } } public void Clear()
{
first = null; last = null; count = 0;
}
public int Count { get { return count; } } public bool Contains(T data)
{
Node<T> current = first;
if (current == null) return false; do
{
if (current.Data.Equals(data)) return true; current = current.Next;
}
while (current != first); return false;
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)this).GetEnumerator();
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
Node<T> current = first; do
{
if (current != null)
{
yield return current.Data; current = current.Next;
}
}
while (current != first);
}
}
}
class Program
{
static void Main(string[] args)
{
var list = new List<int>(); var rand = new Random(); int[] spisok = new int[10];
15
for (int i = 0; i < spisok.Length; i++)
{
spisok[i] = rand.Next(1, 9);
}
foreach (var item in spisok)
{
list.Add(item);
}
foreach (var item in list)
{
Console.WriteLine(item);
}
Console.WriteLine("\nЧто вы хотите сделать с этим списком?"); Console.WriteLine("Удалить четные элементы - 1, удалить повторяющиеся элементы - 2
");
switch (int.Parse(Console.ReadLine()))
{
case 1: delchet(list); break;
case 2: delclon(list); break;
}
}
static void delchet(List<int> list)
{
int[] f = new int[list.Count]; int k = 0;
foreach (var item in list)
{
f[k] = item; k++;
}
for (int i = 0; i < f.Length; i++)
{
if (f[i] % 2 == 0)
{
list.Remove(f[i]);
}
}
Enter(list);
}
static void delclon(List<int> list)
{
int[] f = new int[list.Count]; int k = 0;
foreach (var item in list)
{
f[k] = item; k++;
}
for (int i = 0; i < f.Length; i++)
{
for (int j = 1; j < f.Length; j++)
{
16
if (i != j)
{
if (f[i] == f[j])
{
list.Remove(f[i]);
list.Remove(f[j]);
}
}
}
}
Enter(list);
}
static void Enter(List<int> list)
{
Console.WriteLine("\n После операций: \n"); foreach (var t in list)
{
Console.WriteLine(t);
}
}
}
17