publicclassNode{
privateobject data;publicobject Data
{
get=> data;set=> data =value;}privateNode next;publicNode Next
{
get=> next;set=> next =value;}publicNode(){
}publicNode(object _data){
Data = _data;
Next =null;}}
SingleLink链表类
usingUnityEngine;publicclassSingleLink:ILink{
privateNode head;publicNode Head
{
get=> head;privateset=> head =value;}publicSingleLink(){
Head =null;}publicvoidAppend(Node item){
if(Head ==null){
Head = item;return;}Node t = Head;while(t.Next !=null){
t = t.Next;}
t.Next = item;}publicNodeDelete(int index){
if(index <1|| index >GetLength()){
Debug.LogErrorFormat("删除位置有误{0}", index);returndefault;}if(IsEmpty()){
Debug.LogError("链表为空");returndefault;}Node q =newNode();if(index ==1){
q = head;
head = q.Next;return q;}Node t = Head;int j =1;while(t.Next !=null&& j < index){
q = t;
t = t.Next;
j++;}if(j == index){
q.Next = t.Next;return t;}returndefault;}publicvoidInsert(int index,Node node){
if(index <1|| index >GetLength()){
Debug.LogErrorFormat("插入位置有误{0}", index);return;}if(IsEmpty()){
Debug.LogError("链表为空");return;}Node t = Head;int j =1;while(t.Next !=null&& j < index){
t = t.Next;
j++;}if(j == index){
node.Next = t.Next;
t.Next = node;}}publicNodeGetElement(int index){
Node result =default;if(index <1|| index >GetLength()){
Debug.LogError("查询元素的位置索引不对");return result;}Node t = head;int j =1;while(t.Next !=null&& j < index){
t = t.Next;
j++;}if(j == index){
result = t;}return result;}publicvoidClear(){
Head =null;}publicboolIsEmpty(){
return Head ==null;}publicintGetLength(){
int count =0;Node t = Head;while(t !=null){
count++;
t = t.Next;}return count;}}