版权声明:本人菜鸟一只,如文章有错误或您有高见,请不吝赐教 https://blog.csdn.net/qq_41138935/article/details/82740546
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 单链表
{
class LinkedNode<T>
{
T data; //数据域,当前结点的数据
LinkedNode<T> next;//引用域,下一个结点
public LinkedNode(T val) //构造器
{
data = val;
next = null;
}
public T Data //属性
{
get { return data; }
set { data = value; }
}
public LinkedNode<T> Next //属性
{
get { return next; }
set { next = value; }
}
}
class LinkedStructure<T>
{
LinkedNode<T> first; //头结点
LinkedNode<T> current; //当前结点
public LinkedStructure() //头结点为空,即,空链表
{
first = null;
}
public void addData(LinkedNode<T> var) //var 是对结点的引用
{
if (first == null)
{
first = var;
current = var;
return;
}
else
{
current.Next = var;
current = var;
}
}
public void displayData() //打印链表里的值
{
current = first;
while (current != null)
{
Console.Write(current.Data+" ");
current = current.Next;
}
}
}
class Program
{
static void Main(string[] args)
{
string s1 = "red";
string s2 = "blue";
string s3 = "yellow";
LinkedStructure<String> l = new LinkedStructure<string>();
l.addData(new LinkedNode<string>(s1));
l.addData(new LinkedNode<string>(s2));
l.addData(new LinkedNode<string>(s3));
l.displayData();
Console.ReadKey();
}
}
}