using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 链表
{
class LinkedNode<T>
{
public T value;
public LinkedNode<T> nextNode;
public LinkedNode(T value)
{
this.value = value;
}
}
class LinkedList<T>
{
public LinkedNode<T> head;
public LinkedNode<T> last;
public void Add(T value)
{
LinkedNode<T> node = new LinkedNode<T>(value);
if (head == null)
{
head = node;
last = node;
}
else
{
last.nextNode = node;
last = node;
}
}
public void Remove(T value)
{
if (head == null)
{
return;
}
if (head.value.Equals(value))
{
head = head.nextNode;
if (head == null)
{
last = null;
}
return;
}
LinkedNode<T> node = head;
while (node.nextNode != null)
{
if (node.nextNode.value.Equals(value))
{
node.nextNode = node.nextNode.nextNode;
break;
}
node = node.nextNode;
}
}
}
class Program
{
static void Main(string[] args)
{
LinkedList<int> link = new LinkedList<int>();
link.Add(1);
link.Add(2);
link.Add(3);
link.Add(4);
link.Add(5);
LinkedNode<int> node3 = link.head;
while (node3 != null)
{
Console.WriteLine(node3.value);
node3 = node3.nextNode;
}
link.Remove(3);
node3 = link.head;
while (node3 != null)
{
Console.WriteLine(node3.value);
node3 = node3.nextNode;
}
}
}
}
C# 简单链表
于 2024-08-28 14:34:15 首次发布