using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1.链表
{
public class Node<T>
{
public T Value { get; set; }
public Node<T> nextNode { get; set; }
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1.链表
{
public class LinkedList<T> : IEnumerable<T>
{
private Node<T> firstNode { get; set; }
public void Update(T oldValue, T newValue)
{
Find(oldValue).Value = newValue;
}
public void Add(T value)
{
if (firstNode == null)
{
firstNode = new Node<T> { Value = value };
return;
}
Node<T> current = firstNode;
while (current.nextNode != null)
{
current = current.nextNode;
}
current.nextNode = new Node<T> { Value = value };
}
public void Clear()
{
firstNode = null;
}
public void Remove(T value)
{
if (firstNode.Value.Equals(value))
{
firstNode = firstNode.nextNode;
return;
}
Node<T> current = firstNode;
while (current.nextNode != null)
{
var temp = current.nextNode;
if (temp.Value.Equals(value))
{
current.nextNode = temp.nextNode ?? null;
break;
}
current = current.nextNode;
}
}
public Node<T> Find(T value)
{
Node<T> current = firstNode;
while (current != null)
{
if (current.Value.Equals(value))
{
return current;
}
current = current.nextNode;
}
return null;
}
public IEnumerator GetEnumerator()
{
Node<T> current = firstNode;
while (current != null)
{
yield return current;
current = current.nextNode;
}
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
yield return (T)GetEnumerator();
}
}
}