版权声明:本人菜鸟一只,如文章有错误或您有高见,请不吝赐教 https://blog.csdn.net/qq_41138935/article/details/83097772
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp8
{
class SNode<T> //结点,单链表
{
private T data; //数据域
private SNode<T> next; //位置域
public SNode(T val,SNode<T> p) //普通结点
{
data = val;
next = p;
}
public SNode(SNode<T> p) //头结点
{
next = p;
}
public SNode(T val) //尾结点
{
data = val;
next = null;
}
public SNode() { }
public T Data
{
get { return data; }
set { data = value; }
}
public SNode<T> Next
{
get { return next; }
set { next = value; }
}
}
class SLinkList<T>:SNode<T>
{
private SNode<T> start; //头引用
int length; //单链表的长度
//初始化单链表
public SLinkList()
{
start = null;
}
//在单链表的末尾追加数据元素
public void InsertNode(T a)
{
if (start == null)
{
start = new SNode<T>(a);
length++;
return;
}
SNode<T> current = start;
while (current.Next != null)
{
current=current.Next;
}
current.Next = new SNode<T>(a);
length++;
}
//在单链表的第i个数据元素的位置插入一个数据元素
public void InsertNode(T a,int i)
{
SNode<T> current; //当前结点
SNode<T> previous; //上一个结点
if (i < 1 || i > length + 1)
{
Console.WriteLine("Position is error!");
return;
}
SNode<T> newnode = new SNode<T>(a);
//在空链表或第一个元素前插入第一个元素
if (i == 1)
{
newnode.Next = start;
start = newnode;
length++;
return;
}
//单链表的两个元素之间插入一个元素
current = start;
previous = null;
int j = 1;
while (current!=null&&j<i)
{
previous = current;
current = current.Next;
j++;
}
if (j == i)
{
newnode.Next = current;
previous.Next = newnode;
length++;
}
}
//删除单链表的第i个数据元素
public void DeleteNode(int i)
{
if (IsEmpty() || i < 1)
{
Console.WriteLine("Link is empty or Position is error!");
}
SNode<T> current = start;
if (i == 1)
{
start = current.Next;
length--;
return;
}
SNode<T> previous = null;
int j = 1;
while (current.Next != null && j < i)
{
previous = current;
current = current.Next;
j++;
}
if (j == i)
{
previous.Next = current.Next;
current = null;
length--;
}
else
{
Console.WriteLine("The node is not exist!");
}
}
//获得单链表的第i个数据元素
public T SearchNode(int i)
{
if (IsEmpty())
{
Console.WriteLine("List is empty!");
return default(T);
}
SNode<T> current = start;
int j = 1;
while (current.Next != null && j < i) {
current = current.Next;
j++;
}
if (j == i)
{
return current.Data;
}
else
{
Console.WriteLine("The node is not exist!");
return default(T);
}
}
//在单链表中查找值为value的数据元素
public T SearchNode(T value)
{
if (IsEmpty())
{
Console.WriteLine("List is Empty!");
return default(T);
}
SNode<T> current = start;
while (!current.Data.ToString().Contains(value.ToString()) && current != null)
{
current = current.Next;
}
if (current != null)
{
return current.Data;
}
else
{
return default(T);
}
}
//求单链表的长度
public int GetLength()
{
return length;
}
//清空单链表
public void Clear()
{
start = null;
}
//判断单链表是否为空
public bool IsEmpty()
{
if (start == null)
{
return true;
}
else
{
return false;
}
}
//打印链表元素
public void print_f()
{
//Console.Clear();
if (IsEmpty())
{
Console.WriteLine("List is empty!");
return;
}
SNode<T> current = start;
if (start == null)
{
Console.WriteLine("List is empty!");
return;
}
while (current.Next != null)
{
Console.WriteLine(current.Data);
current = current.Next;
}
Console.WriteLine(current.Data);
Console.WriteLine("------------------------------");
}
}
class Program
{
static void Main(string[] args)
{
SLinkList<string> str = new SLinkList<string>();
str.InsertNode("a"); //末尾追加元素
str.InsertNode("b"); //末尾追加元素
str.InsertNode("c"); //末尾追加元素
str.print_f(); //打印单链表
str.InsertNode("1",1); //在位置1插入元素1
str.InsertNode("3", 3); //在位置1插入元素3
str.print_f(); //打印单链表
str.DeleteNode(2); //删除第二个元素
str.print_f(); //打印单链表
Console.WriteLine("单链表的第3个元素:"+str.SearchNode(3));
Console.WriteLine("单链表的长度为:"+str.GetLength());
str.print_f(); //打印单链表
str.Clear(); //清空链表
str.print_f(); //打印单链表
Console.ReadKey();
}
}
}