Unity3D中单链表实现

Unity3D中单链表实现

 

 

 

using UnityEngine;

using System.Collections;



//单链表结点类,采用泛型
public class Node
{
    private T data; //数据域,当前结点的数据
    private Node next; //引用域,即下一结点

    //构造器:数据域+引用域,普通结点
    public Node(T item, Node p)
    {
        data = item;
        next = p;
    }

    //构造器:引用域,头结点
    public Node(Node p)
    {
        next = p;
    }

    //构造器:数据域,尾结点
    public Node(T val)
    {
        data = val;
        next = null;
    }

    //构造器:无参数
    public Node()
    {
        data = default(T);
        next = null;
    }

    //数据域属性
    public T Data
    {
        get
        {
            return data;
        }
        set
        {
            data = value;
        }
    }

    //引用域属性
    public Node Next
    {
        get
        {
            return next;
        }
        set
        {
            next = value;
        }
    }
}

//链表类,包含链表定义及基本操作方法
public class MyLinkList
{
    private Node head; //单链表的头结点

    //头结点属性
    public Node Head
    {
        get
        {
            return head;
        }
        set
        {
            head = value;
        }
    }


    //构造器
    public MyLinkList()
    {
        head = null;
    }

    //求单链表的长度
    public int GetLength()
    {
        Node p = head;
        int len = 0;
        while (p != null)
        {
            ++len;
            p = p.Next;
        }
        return len;
    }

    //清空单链表
    public void Clear()
    {
        head = null;
    }
    //判断单链表是否为空
    public bool IsEmpty()
    {
        if (head == null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //在单链表的末尾添加新元素
    public void Append(T item)
    {
        Node q = new Node(item);
        Node p = new Node();
        if (head == null)
        {
            head = q;
            return;
        }
        p = head;
        while (p.Next != null)
        {
            p = p.Next;
        }
        p.Next = q;
    }

    //在单链表的第i个结点的位置前插入一个值为item的结点
    public void Insert(T item, int i)
    {
        if (IsEmpty() || i < 1 || i >GetLength())
        {
            Debug.Log("LinkList is empty or Positionis error!");
            return;
        }
        if (i == 1)
        {
            Node q = new Node(item);
            q.Next = head;
            head = q;
            return;
        }
        Node p = head;
        Node r = new Node();
        int j = 1;
        while (p.Next != null && j < i)
        {
            r = p;
            p = p.Next;
            ++j;
        }
        if (j == i)
        {
            Node q = new Node(item);
            q.Next = p;
            r.Next = q;
        }
    }

    //在单链表的第i个结点的位置后插入一个值为item的结点
    public void InsertPost(T item, int i)
    {
        if (IsEmpty() || i < 1 || i >GetLength())
        {
            Debug.Log("LinkList is empty or Positionis error!");
            return;
        }
        if (i == 1)
        {
            Node q = new Node(item);
            q.Next = head.Next;
            head.Next = q;
            return;
        }
        Node p = head;
        int j = 1;
        while (p != null && j < i)
        {
            p = p.Next;
            ++j;
        }
        if (j == i)
        {
            Node q = new Node(item);
            q.Next = p.Next;
            p.Next = q;
        }
    }

    //删除单链表的第i个结点
    public T Delete(int i)
    {
        if (IsEmpty() || i < 0 || i >GetLength())
        {
            Debug.Log("LinkList is empty or Positionis error!");
            return default(T);
        }
        Node q = new Node();
        if (i == 1)
        {
            q = head;
            head = head.Next;
            return q.Data;
        }
        Node p = head;
        int j = 1;
        while (p.Next != null && j < i)
        {
            ++j;
            q = p;
            p = p.Next;
        }
        if (j == i)
        {
            q.Next = p.Next;
            return p.Data;
        }
        else
        {
            Debug.Log("The " + i + "th node is not exist!");
            return default(T);
        }
    }

    //获得单链表的第i个数据元素
    public T GetElem(int i)
    {
        if (IsEmpty() || i < 0)
        {
            Debug.Log("LinkList is empty or positionis error! ");
            return default(T);
        }
        Node p = new Node();
        p = head;
        int j = 1;
        while (p.Next != null && j < i)
        {
            ++j;
            p = p.Next;
        }
        if (j == i)
        {
            return p.Data;
        }
        else
        {
            Debug.Log("The " + i + "th node is not exist!");
            return default(T);
        }
    }

    //在单链表中查找值为value的结点
    public int Locate(T value)
    {
        if (IsEmpty())
        {
            Debug.Log("LinkList is Empty!");
            return -1;
        }
        Node p = new Node();
        p = head;
        int i = 1;
        while (!p.Data.Equals(value) &&p.Next != null)
        {
            p = p.Next;
            ++i;
        }
        return i;
    }

    //显示链表
    public void Display()
    {
        Node p = new Node();
        p = this.head;
        while (p != null)
        {
            Debug.Log(p.Data + " ");
            p = p.Next;
        }
    }
}

 

 

实例测试程序代码:

 

   

void Start ()
    {
        MyLinkList myLinkList = new MyLinkList(); //实例化一个单链表 
        Debug.Log(myLinkList.GetLength());   //获取长度 

        //添加元素 
        myLinkList.Append("good");
        myLinkList.Append("monring");
        myLinkList.Append("lwk");

        myLinkList.Insert("!", 5);  //在i结点前插元素,位置错误测试 
        myLinkList.InsertPost("!", 5);  //在i结点后插元素,位置错误测试 
        myLinkList.InsertPost("!", 3);  //后插元素 
        myLinkList.Insert(",", 3);  //前插元素 

        myLinkList.Display();  //显示链表元素 
        Debug.Log(myLinkList.GetElem(4));//获取结点,并显示 

        myLinkList.Delete(1);  //删除结点
        myLinkList.Display();

        Debug.Log(myLinkList.GetLength()); //显示链表长度 
}   

 

截图部分:

 

Unity3D中单链表实现_第1张图片

 

 

博客链接:   http://blog.csdn.net/m1234567q

转载请注明:   阿成 201609月19  发表

 

参考网址: http://blog.csdn.net/lwk2011/article/details/5951701

 

 

 

你可能感兴趣的:(C#)