单链表-普通

最基本的单链表

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;
    }

}

无泛型,不能迭代

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值