Реализация списка посредством указателей - C#
Формулировка задачи:
Здравствуйте!
Собственно, так и звучит задание.
В методичке примеры на Паскале
и так далее...
Пожалуйста, помогите реализовать на C#
Type
Spisok=^pSpisok; {для соответствия типа ячейки и типа хранящегося адреса в поле next}
pSpisok=record
element: string;
next: Spisok;
end;
Var p0, p, p1, p2, q: Spisok;
Begin
New(p0); {создание пустой ячейки без имени, но с адресом (рис.1.3), который помещается в ячейку p0}
p0^.element:=’header’;{поместили x= header в поле element ячейки с адресом p0}
p0^.next:=nil;{поместили знак конца списка nil в поле next ячейки p0}
End;
Procedure INSERT;
Begin
Repeat
Writeln(‘Enter element? y/n’);
Readln(b);
If b=’y’ then
begin
q:=p1; {копируется адрес из одной ячейки в другую, т.к при следующем выполнении new(p1), предыдущий адрес ячейки заменятся новым адресом}
New(p1); {создаётся новая ячейка рис. 1.5}
Writeln(‘element’);
Readln(x);
p1^.element:=x;
p1^.next:=nil;
q^.next:=p1; {соединяется предыдущая ячейка с адресом q, там копия её адреса, и новая ячейка рис. 1.6}
End;
Until b<>’y’;
End;Решение задачи: «Реализация списка посредством указателей»
textual
Листинг программы
internal class Node<T>
{
public Node<T> Next { get; set; }
public T Value { get; set; }
}
class LinkedList<T> : System.Collections.Generic.ICollection<T>
{
Node<T> head;
Node<T> tail;
int count = 0;
public void Add(T item)
{
if (this.head == null)
{
this.head = new Node<T>();
this.head.Value = item;
this.tail = this.head;
}
else
{
this.tail.Next = new Node<T>();
this.tail.Next.Value = item;
this.tail = this.tail.Next;
}
this.count++;
}
public void Clear()
{
this.head = null;
this.tail = null;
this.count = 0;
}
public bool Contains(T item)
{
Node<T> current = this.head;
while (current != null)
{
if (current.Value.Equals(item))
return true;
current = current.Next;
}
return false;
}
public void CopyTo(T[] array, int arrayIndex)
{
Node<T> current = this.head;
while (current != null)
{
array[arrayIndex++] = current.Value;
current = current.Next;
}
}
public int Count
{
get
{
return this.count;
}
}
public bool IsReadOnly
{
get
{
return false;
}
}
public bool Remove(T item)
{
if (count == 0)
return false;
bool result = false;
Node<T> current = this.head;
while (current.Next != null)
{
if (current.Next.Value.Equals(item))
{
result = true;
current.Next = current.Next.Next;
}
current = current.Next;
}
if (this.head.Value.Equals(item))
{
result = true;
this.head = this.head.Next;
}
if (this.head.Next != null && this.head.Next.Value.Equals(item))
{
result = true;
this.head.Next = null;
}
return result;
}
public System.Collections.Generic.IEnumerator<T> GetEnumerator()
{
return Enumerate().GetEnumerator();
}
private System.Collections.Generic.IEnumerable<T> Enumerate()
{
Node<T> current = this.head;
while (current != null)
{
T value = current.Value;
current = current.Next;
yield return value;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return Enumerate().GetEnumerator();
}
}