单链表-普通

最基本的单链表

ILink接口

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

Node节点

public class Node
{
    
    
    private object data;
    public object Data
    {
    
    
        get => data;
        set => data = value;
    }
    private Node next;
    public Node Next
    {
    
    
        get => next;
        set => next = value;
    }
    public Node()
    {
    
    

    }
    public Node(object _data)
    {
    
    
        Data = _data;
        Next = null;
    }
}

SingleLink链表类

using UnityEngine;

public class SingleLink : ILink
{
    
    
    private Node head;
    public Node Head
    {
    
    
        get => head;
        private set => head = value;
    }

    public SingleLink()
    {
    
    
        Head = null;
    }
    public void Append(Node item)
    {
    
    
        if (Head == null)
        {
    
    
            Head = item;
            return;
        }
        Node t = Head;
        while (t.Next != null)
        {
    
    
            t = t.Next;
        }
        t.Next = item;

    }

    public Node Delete(int index)
    {
    
    
        if (index < 1 || index > GetLength())
        {
    
    
            Debug.LogErrorFormat("删除位置有误{0}", index);
            return default;
        }
        if (IsEmpty())
        {
    
    
            Debug.LogError("链表为空");
            return default;
        }
        Node q = new Node();
        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;
        }
        return default;
    }

    public void Insert(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;
        }

    }
    public Node GetElement(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;
    }
    public void Clear()
    {
    
    
        Head = null;
    }

    public bool IsEmpty()
    {
    
    
        return Head == null;
    }

    public int GetLength()
    {
    
    
        int count = 0;
        Node t = Head;
        while (t != null)
        {
    
    
            count++;
            t = t.Next;
        }
        return count;
    }

}

无泛型,不能迭代

猜你喜欢

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