Циклический список - C# (213314)
Формулировка задачи:
на 40й строке выдает ошибку
как исправить?
Operator '==' cannot be applied to operands of type 'int' and 'CycleList.Program.Element'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CycleList
{
class Program
{
class Element
{
public int value;
public Element next;
public Element(int value)
{
this.value = value;
}
}
class List
{
public Element first, last, current;
public List()
{
first = null;
last = first;
current = null;
}
public void Add(Element el)
{
if (last != null)
{
last.next = el;
}
last = el;
if (first == null)
first = current = el;
}
public void Remove(Element el)
{
while (last.next != null && last.value == el)
{
last.next = last.next.next;
}
if (last.next != null)
last.Remove(el);
}
}
static void Main(string[] args)
{
List l = new List();
Element el;
for (int i = 1; i < 12; i++)
{
el = new Element(i);
l.Add(el);
}
while (l.current.next != null)
{
Console.WriteLine(l.current.value);
l.current = l.current.next;
Console.WriteLine();
}
Console.WriteLine(l.first.value);
Console.WriteLine(l.last.value);
Console.ReadLine();
}
}
}Решение задачи: «Циклический список»
textual
Листинг программы
public void Remove(int value) // Удалять надо по значению, а не по элементу. Пользователю класса вовсе необязательно знать о внутренней "кухне".
{
if (first == null) return; // Список пуст
Element curr = first;
Element prev = null;
do
{
if (curr.value == value) // Значение найдено
{
if (curr == first) { first = first.Next; last.Next = first; }
else if (curr == last) { last = prev; last.Next = first; }
else prev.Next = curr.Next;
return;
}
prev = curr;
}
while ((curr = curr.Next) != first);
}