单链表——泛型

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>
{
    
    
    /// <summary>
    /// 增加
    /// </summary>
    void Append(T item);
    /// <summary>
    /// 删除
    /// </summary>
    T Delete(int index);
    /// <summary>
    /// 插入
    /// </summary>
    /// <param name="index">要插入的位置索引</param>
    /// <param name="node">要插入的对象</param>
    void Insert(int index, T item);
    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="index">要查询的索引</param>
    /// <returns></returns>
    T GetElement(int index);
    /// <summary>
    /// 清空整个链表
    /// </summary>
    void Clear();
    /// <summary>
    /// 链表是否为空
    /// </summary>
    /// <returns></returns>
    bool IsEmpty();
    /// <summary>
    /// 获取链表长度
    /// </summary>
    /// <returns></returns>
    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;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_39691716/article/details/120999633