Универсальные шаблоны(сравнение) - C#
Формулировка задачи:
Доброго вечера. Необходимо реализовать метод нахождения максимального и минимального элемента в структуре дека(deque). Класс реализован, а как сравнить 2 значения не знаю.
using System;
using System.Collections.Generic;
namespace Universal_Pattern18
{
class Program
{
public class Node<T>
{
public Node(T data)
{
Data = data;
}
public T Data { get; set; }
public Node<T> Previous { get; set; }
public Node<T> Next { get; set; }
}
public class Deque<T> // двусвязный список
{
Node<T> head; // первый элемент
Node<T> tail; // последний элемент
int count; // количество элементов в списке
//добавление
public void AddLast(T data)
{
Node<T> node = new Node<T>(data);
if (head == null)
head = node;
else
{
tail.Next = node;
node.Previous = tail;
}
tail = node;
count++;
}
public void AddFirst(T data)
{
Node<T> node = new Node<T>(data);
Node<T> temp = head;
node.Next = temp;
head = node;
if (count == 0)
tail = head;
else
temp.Previous = node;
count++;
}
//удаление
public T RemoveFirst()
{
T output = head.Data;
if (count == 1)
{
head = tail = null;
}
else
{
head = head.Next;
head.Previous = null;
}
count--;
return output;
}
public T RemoveLast()
{
T output = tail.Data;
if (count == 1)
{
head = tail = null;
}
else
{
tail = tail.Previous;
tail.Next = null;
}
count--;
return output;
}
public T First
{
get
{
return head.Data;
}
}
public T Last
{
get
{
return tail.Data;
}
}
public int Count { get { return count; } }
public bool Contains(T data)
{
Node<T> current = head;
while (current != null)
{
if (current.Data.Equals(data))
return true;
current = current.Next;
}
return false;
}
public IEnumerator<T> GetEnumerator()
{
Node<T> current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
}
public static T Max<T>(Deque<T> deq)
{
T max = deq.First;
foreach(T elem in deq)
{
if (elem > max)
max = elem;
}
return max;
}
static void Main(string[] args)
{
Deque<string> users = new Deque<string>();
users.AddFirst("Наталья");
users.AddLast("Артем");
users.AddLast("Виктор");
foreach (string s in users)
Console.WriteLine(s);
string removedItem = users.RemoveLast();
Console.WriteLine("\n Удален: {0} \n", removedItem);
foreach (string s in users)
Console.WriteLine(s);
Console.Read();
}
}
}Решение задачи: «Универсальные шаблоны(сравнение)»
textual
Листинг программы
public class Node<T> where T : IComparable<T>
{
// ...
}
public class Deque<T> where T : IComparable<T>
{
// ...
}