Unity 用C#写的链表类
用于保存坐标点
using UnityEngine;
using System;
using System.Collections;
/// <summary>
/// 结点类
namespace CS
{
/// </summary>
public class ListNode
{
public Vector3 data; //ElemType
public ListNode(){} // 构造函数
public ListNode next;
}
/// <summary>
/// 链表类
/// </summary>
public class LinkList{
private ListNode first; //第一个结点
public LinkList(){
first = null;
}
public bool IsEmpty()
{
return first == null;
}
/// <summary>
/// 在第k个元素之后插入x
/// </summary>
/// <param name="k"></param>
/// <param name="x"></param>
/// <returns></returns>
public LinkList Insert( int k, Vector3 x )
{
if( k<0 )
return null;
ListNode pNode = first; //pNode将最终指向第k个结点
for( int index = 1; index<k && pNode != null; index++ )
pNode = pNode.next;
if( k>0 && pNode == null )
return null;//不存在第k个元素
ListNode xNode = new ListNode();
xNode.data = x;
if( k>0 )
{
//在pNode之后插入
xNode.next = pNode.next;
pNode.next = xNode;
}
else
{
//作为第一个元素插入
xNode.next = first;
first = xNode;
}
return this;
}
public int Length()
{
ListNode current = first;
int length = 0;
while(current != null)
{
length++;
current = current.next;
}
return length;
}
/// <summary>
/// 返回第k个元素至x中
/// </summary>
/// <param name="k"></param>
/// <param name="x"></param>
/// <returns>如果不存在第k个元素则返回false,否则返回true</returns>
public bool Find( int k, ref Vector3 x )
{
if( k<1 )
return false;
ListNode current = first;
int index = 1;
while( index<k && current != null )
{
current = current.next;
index++;
}
if( current != null )
{
x = current.data;
return true;
}
return false;
}
/// <summary>
/// 返回x所在的位置
/// </summary>
/// <param name="x"></param>
/// <returns>如果x不在表中则返回0</returns>
public int Search( Vector3 x )
{
ListNode current = first;
int index = 1;
while( current != null && current.data !=x )
{
current = current.next;
index++;
}
if(current != null)
return index;
return 0;
}
/// <summary>
/// 删除第k个元素,并用x返回其值
/// </summary>
/// <param name="k"></param>
/// <param name="x"></param>
/// <returns></returns>
public LinkList Delete( int k, ref Vector3 x )
{
//如果不存在第k个元素则引发异常
if( k<1 || first == null )
return null;
ListNode pNode = first; //pNode将最终指向第k个结点
//将pNode移动至第k个元素,并从链表中删除该元素
if( k == 1 ) //pNode已经指向第k个元素
first = first.next; //删除之
else
{
//用qNode指向第k-1个元素
ListNode qNode = first;
for( int index=1; index< k-1 && qNode != null; index++ )
qNode = qNode.next;
if( qNode == null || qNode.next == null )
return null;//不存在第k个元素
pNode = qNode.next; //pNode指向第k个元素
qNode.next = pNode.next; //从链表中删除第k个元素
x = pNode.data;
}
return this;
}
// 清空链表
public void Clear()
{
first = null;
}
}
}