Node节点类
public class Node< T>
{
private T data;
public T Data
{
get => data;
set => data = value ;
}
private Node< T> next;
public Node< T> Next
{
get => next;
set => next = value ;
}
public Node ( )
{
}
public Node ( T _data)
{
Data = _data;
Next = null ;
}
}
ILink接口
using System. Collections ;
using System. Collections. Generic ;
public interface ILink< T>
{
void Append ( T item) ;
T Delete ( int index) ;
void Insert ( int index, T item) ;
T GetElement ( int index) ;
void Clear ( ) ;
bool IsEmpty ( ) ;
int GetLength ( ) ;
}
SingleLink单链表类
using System. Collections ;
using UnityEngine ;
public class SingleLink< T> : IEnumerable , ILink< T>
{
private Node< T> head;
public Node< T> Head
{
get => head;
private set => head = value ;
}
public SingleLink ( )
{
Head = null ;
}
public void Append ( T item)
{
Node< T> q = new Node< T> ( item) ;
if ( head == null )
{
head = q;
return ;
}
Node< T> t = head;
while ( t. Next != null )
{
t = t. Next;
}
t. Next = q;
}
public T Delete ( int index) {
if ( index < 1 || index > GetLength ( ) )
{
Debug. LogErrorFormat ( "删除位置有误{0}" , index) ;
return default ( T ) ;
}
if ( IsEmpty ( ) )
{
Debug. LogError ( "链表为空" ) ;
return default ( T ) ;
}
Node< T> q = new Node< T> ( ) ;
if ( index == 1 )
{
q = head;
head = q. Next;
return q. Data;
}
Node< T> 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 q. Data;
}
return default ( T ) ;
}
public void Insert ( int index, T item)
{
if ( index < 1 || index > GetLength ( ) )
{
Debug. LogErrorFormat ( "删除位置有误{0}" , index) ;
return ;
}
if ( IsEmpty ( ) )
{
Debug. LogError ( "链表为空" ) ;
return ;
}
Node< T> q = new Node< T> ( item) ;
Node< T> t = head;
int j = 1 ;
while ( t. Next != null && j < index)
{
t = t. Next;
j++ ;
}
if ( j == index)
{
q. Next = t. Next;
t. Next = q;
}
}
public T GetElement ( int index)
{
if ( index < 1 || index > GetLength ( ) )
{
Debug. LogErrorFormat ( "查询位置有误{0}" , index) ;
return default ( T ) ;
}
if ( IsEmpty ( ) )
{
Debug. LogError ( "链表为空" ) ;
return default ( T ) ;
}
Node< T> t = head;
int j = 1 ;
while ( t. Next != null && j < index )
{
t = t. Next;
j++ ;
}
if ( j == index)
{
return t. Data;
}
return default ( T ) ;
}
public void Clear ( )
{
Head = null ;
}
public bool IsEmpty ( )
{
return Head == null ;
}
public int GetLength ( )
{
int count = 0 ;
Node< T> t = Head;
while ( t != null )
{
count++ ;
t = t. Next;
}
return count;
}
public IEnumerator GetEnumerator ( )
{
return new QIenumerator< T> ( this ) ;
}
}
迭代器类QIenumerator
using System. Collections ;
using System. Collections. Generic ;
using UnityEngine ;
public class QIenumerator< T> : IEnumerator< T>
{
private ILink< T> _list;
private int _index;
private T _current;
public T Current => _current;
object IEnumerator. Current => Current;
public QIenumerator ( ILink< T> list)
{
_list = list;
_index = 0 ;
}
public void Dispose ( )
{
}
public bool MoveNext ( )
{
if ( _index < _list. GetLength ( ) )
{
_current = _list. GetElement ( ++ _index) ;
return true ;
}
return false ;
}
public void Reset ( )
{
_current = default ( T ) ;
_index = 0 ;
}
}