【數(shù)據(jù)結(jié)構(gòu)】【C#】002-線性表:??單鏈表

C#數(shù)據(jù)結(jié)構(gòu):?jiǎn)捂湵?/h1>
  • 1、自定義單鏈表結(jié)構(gòu):
  • 單鏈節(jié)點(diǎn)類
using System.Collections;
using System.Collections.Generic;
using UnityEngine;




/// <summary>
/// 單鏈表節(jié)點(diǎn)
/// </summary>
/// <typeparam name="T"></typeparam>
public class Node<T>
{
    private T data;//數(shù)據(jù)

    private Node<T> next; //指針,下個(gè)元素

    public T Data
    {
        get
        {
            return data;
        }

        set
        {
            data = value;
        }
    }

    public Node<T> Next
    {
        get
        {
            return next;
        }

        set
        {
            next = value;
        }
    }

    public Node()
    {
        data = default(T);
        next = null;
    }

    public Node(T _data, Node<T> _next)
    {
        this.data = _data;
        this.next = _next;
    }

    public Node(T _data)
    {
        this.data = _data;
        this.next = null;
    }

    public Node(Node<T> _next)
    {
        this.next = _next;
    }
       
}
  • 單鏈表類

/// <summary>
/// 單鏈表
/// </summary>
/// <typeparam name="T"></typeparam>
public class LinkList<T> 
{
    private Node<T> head;//頭指針

    public LinkList()
    {
        head = new Node<T>();
    }

    

    //判空
    public bool IsEmpty()
    {
        return head.Next == null;
    }

    //添加操作
    public void Add(T item,bool isHeadAdd=false)
    {
        if(isHeadAdd)
        {
            Insert(item, 0);
            return;
        }

        if(IsEmpty())
        {
            head.Next = new Node<T>(item);
        }
        else
        {
            Node<T> temp = head;
            while (true)
            {
                if(temp.Next!=null)
                {
                    temp = temp.Next;
                }
                else
                {
                    break;
                }
            }
            temp.Next = new Node<T>(item);
        }
    }


    //插入操作
    public void Insert(T item, int index)
    {
        if (index < 0 || index > GetLength()) //可以插到尾部
        {
            Debug.LogError("index不合法!");
            return;
        }

        Node<T> newNode = new Node<T>(item);
        if(index==0)//頭插入
        {
            Node<T> temp = head.Next;
            head.Next = newNode;
            newNode.Next = temp;
        }
        else
        {
            Node<T> temp = head;
            for (int i = 0; i < index ; i++)
            {
                temp = temp.Next;
            }
            Node<T> preNode = temp;
            Node<T> currteNode = temp.Next;
            preNode.Next = newNode;
            newNode.Next = currteNode;
        }
    }

    

    //刪除操作
    public T Delete(int index)
    {
        T data = default(T);
        if (index < 0 || index > GetLength()-1)
        {
            Debug.LogError("index不合法!");
            return data;
        }

        Node<T> temp = head;
        for (int i = 0; i < index; i++)
        {
            temp = temp.Next;
        }
        Node<T> preNode = temp;
        Node<T> currteNode = temp.Next;
        preNode.Next = currteNode.Next;
        data = currteNode.Data;
        currteNode = null;
        return data;
    }


    public T this[int index]//索引器訪問(wèn)值
    {
        get
        {
            T data = default(T);
            if (index < 0 || index > GetLength() - 1)
            {
                Debug.LogError("index不合法!");
                return data;
            }

            Node<T> temp = head;
            for(int i=0;i<=index;i++)
            {
                temp = temp.Next;
            }
            return temp.Data;
        }
    }

    //訪問(wèn)index位置的值
    public T GetElem(int index)
    {
        return this[index];
    }


    //鏈表長(zhǎng)度
    public int GetLength()
    {
        int length = 0;
        if(!IsEmpty())
        {
            Node<T> temp = head;
            while (true)
            {
                if (temp.Next != null)
                {
                    length++;
                    temp = temp.Next;                    
                }
                else
                {
                    break;
                }
            }
        }
        return length;
    }

   
    //尋址
    public int Locate(T value)//有相同值的返回首次查找到的元素的index
    {
        if(!IsEmpty())
        {
            int index = 0;
            Node<T> temp = head;
            while(true)
            {
                if(temp.Next!=null)
                {
                    temp = temp.Next;
                    if (temp.Data.Equals(value))
                    {
                        return index;
                    }
                    index++;
                }
                else
                {
                    break;
                }
            }
            Debug.Log("無(wú)此值!");
            return -1;
        }
        else
        {
            Debug.Log("空表!");
            return -1;
        }
        
    }

    //清空操作
    public void Clear()
    {
        head.Next = null;
    }

    //顯示表元素
    public void Display()
    {
        if (IsEmpty())
        {
            Debug.Log("表空");
            return;
        }
        Debug.Log("表中的值:");

        int index = 0;
        Node<T> temp = head;
        while (true)
        {
            if (temp.Next != null)
            {
                temp = temp.Next;
                Debug.Log("index:" + index.ToString() + "   value:" +temp.Data);
                index++;
            }
            else
            {
                break;
            }
        }
    }

   
}

  • 單鏈表測(cè)試用例:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class _002_SingleLinkTable : MonoBehaviour {


   
    LinkList<string> sqeList;

    void Start()
    {
         //初始化順序表
        sqeList = new LinkList<string>();

        ////判空操作
        Debug.Log("單鏈表是否為空:" + sqeList.IsEmpty());



        ////添加操作
        Debug.Log("頭插法,添加操作--------------添加'123','456','789'");
        sqeList.Add("123",true);
        sqeList.Add("456",true);
        sqeList.Add("789",true);
        sqeList.Display();
        Debug.Log("尾插法,添加操作--------------添加'123','456','789'");
        sqeList.Add("123");
        sqeList.Add("456");
        sqeList.Add("789");
        sqeList.Display();

        Debug.Log("單鏈表是否為空:" + sqeList.IsEmpty());


        ////插入操作
        Debug.Log("單鏈表插入操作---------------在index=3處插入字符串:'111'");
        sqeList.Insert("111", 3);
        sqeList.Display();

        ////刪除操作
        sqeList.Delete(2);
        Debug.Log("單鏈表刪除操作---------------刪除index=2的元素");
        sqeList.Display();

        ////表長(zhǎng)
        Debug.Log("單鏈表表長(zhǎng)-------------------單鏈表表長(zhǎng):" + sqeList.GetLength());

        ////查找
        Debug.Log("單鏈表查找--------------index查value");
        Debug.Log("index=0的值:" + sqeList[0]);
        Debug.Log("index=2的值:" + sqeList.GetElem(2));
        Debug.Log("單鏈表查找--------------value查index");

        ////尋址
        Debug.Log("'789’的index值:" + sqeList.Locate("789"));

        ////清空
        Debug.Log("清空單鏈表");
        sqeList.Clear();
        sqeList.Display();
    }


}

運(yùn)行結(jié)果:

img.jpg

注意:

1.單鏈表在訪問(wèn)值時(shí),只能從頭節(jié)點(diǎn)訪問(wèn)下去,只能通過(guò)前一節(jié)點(diǎn)訪問(wèn)到下一節(jié)點(diǎn),很多時(shí)候需要借助臨時(shí)變量來(lái)存儲(chǔ)需要的節(jié)點(diǎn)。

2.單鏈表的添加可以是尾插方式,也可以是頭插法方式.
采用頭插法,創(chuàng)建出的是一個(gè)逆序表,先輸入的在最后。

3.head是表頭指針,head.next=null表示該表為空表,表如果不為空,head.next就是首節(jié)點(diǎn),某個(gè)節(jié)點(diǎn)node.next=null,表示該node為尾節(jié)點(diǎn)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容