en造数据结构与算法C# 使用两个泛型类实现简单的链表
很简单,节点类有头节点,尾节点和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();
}
}
原文地址:https://blog.csdn.net/2301_77947509/article/details/142337263
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!