很简单,节点类有头节点,尾节点和value就可以了
链表类负责增删查改和tostring
using System;
using System.Collections.Generic;
public class Node<T>
{
public T Data { get; set; }
public Node<T> Next { get; set; }
public Node(T data)
{
Data = data;
Next = null;
}
}
public class LinkedList<T>
{
private Node<T> head;
public LinkedList()
{
head = null;
}
public void Add(T data)
{
Node<T> newNode = new Node<T>(data);
if (head == null)
{
head = newNode;
}
else
{
Node<T> current = head;
while (current.Next != null)
{
current = current.Next;
}
current.Next = newNode;
}
}
public void PrintList()
{
Node<T> current = head;
while (current != null)
{
Console.WriteLine(current.Data);
current = current.Next;
}
}
}
class Program
{
static void Main()
{
LinkedList<int> list = new LinkedList<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.PrintList();
}
}