C#核心知识回顾——10.List、Dictionary、数据结构、LinkedList

1.List

 List<int> list = new List<int>();  
        List<String> strings = new List<String>();

        //增
        list.Add(0);
        list.Add(1);

        List<int> ints = new List<int>();
        ints.Add(0);
        list.AddRange(ints);
        //插入
        list.Insert(0, 1);// 位置0插入1

        //删
        //1.移除指定元素
        list.Remove(0);
        //2.移除指定位置元素
        list.RemoveAt(0);
        //3.清空
        list.Clear();

        list.Add(1);
        list.Add(2);
        list.Add(3);
        list.Add(4);
        //查
        //1.得到指定位置的元素
        Debug.Log(list[1]);
        //2.查看元素是否存在
        if (list.Contains(0))
        {
            Debug.Log("存在0");
        }
        //3.正向查找元素位置
        //找到返回位置找不到返回-1
        int index = list.IndexOf(0);
        Debug.Log(index);
        //4.反向查找元素位置
        // 找到返回位置找不到返回 - 1
        int indexlast = list.LastIndexOf(0);
        Debug.Log(indexlast);

        //改
        list[0] = 99;

        //遍历
        for (int i = 0; i < list.Count; i++)
        {
            Debug.Log(list[i]);
        }
        foreach (int item in ints)
        {
            Debug.Log(item);
        }

2.Dictionary:

Dictionary<int,string> dic = new Dictionary<int,string>();

        //增        
        dic.Add(1, "123");
        dic.Add(2, "234");
        dic.Add(3, "345");

        //删
        //1.只能通过键去删除,删除不存在的键没反应
        dic.Remove(1);
        dic.Remove(4);
        //2.清空
        dic.Clear();

        dic.Add(1, "123");
        dic.Add(2, "234");
        dic.Add(3, "345");
        //查
        //1.通过键查看值
        //找不到直接报错!!!!!!
        Debug.Log(dic[1]);
        //2.查看是否存在
        //根据键查
        if (dic.ContainsKey(1)) { Debug.Log("存在键为1"); };
        //根据值查
        if (dic.ContainsValue("123")) { Debug.Log("存在值为123的"); };

        //改
        dic[1] = "999";

        //遍历
        //1.遍历键
        //2.遍历值
        //3.遍历全部
        foreach (var item in dic.Keys)
        {
            Debug.Log(item);
            Debug.Log(dic[item]);
        }
        foreach (var item in dic.Values)
        {
            Debug.Log(item);
        }
        foreach (KeyValuePair<int,string> item in dic)
        {
            Debug.Log("键" + item.Key + "值" + item.Value);
        }

string[] str = { "壹", "贰", "叁", "肆", "伍","陆", "柒", "捌", "玖", "拾" }; 
        Dictionary<string,string> dic = new Dictionary<string,string>();
        for (int i = 0; i < str.Length; i++)
        {
            dic.Add(i.ToString(), str[i]);
        }
        string rang = UnityEngine.Random.Range(0,1000).ToString();
        //Debug.Log(rang);
        foreach (var item in rang)
        {
            if(dic.ContainsKey(item.ToString()))
            { 
                Debug.Log(dic[item.ToString()]);
            }
        }

3.数据结构:
数据结构是计算机存储、组织数据的方式(规则)
数据结构是指相互之间存在一种或多种特定关系的数据元素的集合.
比如自定义的一个类也可以称为一种数据结构自己定义的数据组合规则
简单点理解,就是人定义的存储数据和表示数据之间关系的规则而已

常用的数据结构(前辈总结和制定的一些经典规则)
数组、栈、队列、链表、树、图、堆、散列表(哈希表)

线性表是一种数据结构,是由n个具有相同特性的数据元素的有限序列
比如数组、Array List、Stack、Queue、链表等等

顺序存储和链式存储是数据结构中两种存储结构
数组、Stack、Queue、List、Array List——顺序存储
只是数组、Stack、Queue的组织规则不同而已

顺序存储:
用一组地址连续的存储单元依次存储线性表的各个数据元素

单向链表、双向链表、循环链表一链式存储

链式存储(链接存储):
用一组任意的存储单元存储线性表中的各个数据元素

public class test : MonoBehaviour
{    
    private void Start()
    {
        LinkList<int> linkList = new LinkList<int>();
        linkList.Add(0);
        linkList.Add(1);
        linkList.Add(2);
        linkList.Add(3);
        LinkNode<int> node = linkList.head;
        while (node != null)
        {
            Debug.Log(node.value);
            node = node.nextNode;
        }
        linkList.Remove(2);
        node = linkList.head;
        while (node != null)
        {
            Debug.Log(node.value);
            node = node.nextNode;
        }
        linkList.Remove(0);
        node = linkList.head;
        while (node != null)
        {
            Debug.Log(node.value);
            node = node.nextNode;
        }
    }
}
class LinkNode<T>
{
    public T value;

    public LinkNode<T> nextNode;

    public LinkNode(T t)
    {
        this.value = t;
    }
}
class LinkList<T>
{
    public LinkNode<T> head;
    public LinkNode<T> tail;
    public void Add(T value)
    {
        LinkNode<T> node = new LinkNode<T>(value);
        if(head == null)
        {
            head = node;
            tail =node;
        }
        else
        {
            tail.nextNode = node;
            tail = node;
        }
    }

    public void Remove(T value)
    {
        if(head == null)
        {
            return;
        }
        if (head.value.Equals(value))
        {
            head = head.nextNode;
            如果头结点被移除,发现头结点变空,证明只有一个节点,尾也要置空
            if (head == null)
            {
                tail = null;
            }
            return;
        }
        LinkNode<T> node = head;
        while (node.nextNode != null) { 
            if(node.nextNode.value.Equals(value))
            {
                当前找到的元素上个节点,指向自己的下个节点。
                node.nextNode = node.nextNode.nextNode;
                break;
            }
            node = node.nextNode;
        }
    }
}

顺序存储和链式存储的优缺点

从增删查改的角度去思考:
增:链式存储 计算上 优于顺序存储(中间插入时链式不用像顺序一样去移动位置)
删:链式存储 计算上 优于顺序存储(中间删除时链式不用像顺序一样去移动位置)
查:顺序存储 使用上 优于链式存储(数组可以直接通过下标得到元素,链式需要遍历)
改:顺序存储 使用上 优于链式存储(数组可以直接通过下标得到元素,链式需要遍历)

4.LinkedList

 LinkedList<int> ints = new LinkedList<int>();

        //增
        //1.在链表尾部加元素
        ints.AddFirst(0);
        //2.在链表头部加元素
        ints.AddLast(1);
        ints.AddLast(2);
        ints.AddLast(3);
        ints.AddLast(4);
        //3.在某一个节点之后添加一个节点
        //要指定节点 先得得到一个节点
        LinkedListNode<int> n = ints.Find(2);
        ints.AddAfter(n, 100);
        //4.在某一个节点之前添加一个节点
        //要指定节点 先得得到一个节点
        LinkedListNode<int> m = ints.Find(3);
        ints.AddBefore(m, 120);

        //删
        //1.移除头结点
        ints.RemoveFirst();
        //2.移除尾结点
        ints.RemoveLast();
        //3.移除指定节点
        //无法通过位置直接删除
        ints.Remove(2);
        //4.清空
        ints.Clear();

        ints.AddFirst(0);
        ints.AddLast(1);
        ints.AddLast(2);
        ints.AddLast(3);
        //查
        //1.头结点
        LinkedListNode<int> first = ints.First;
        //2.尾结点
        LinkedListNode<int> last = ints.Last;
        //3.找到指定值节点
        //无法直接通过下标获取中间值
        //只有遍历查找指定位置元素
        LinkedListNode<int> node = ints.Find(1);
        //找不到返回null
        //4.判断是否存在
        if (ints.Contains(0))
        {
            Debug.Log("存在0");
        }

        //改
        ints.First.Value = 10;

        //遍历
        //1.foreach
        foreach (int i in ints)
        {
            Debug.Log(i);
        }
        //2.节点遍历
        //头到尾
        LinkedListNode<int> noeHead = ints.First;
        while (noeHead != null)
        {
            Debug.Log(noeHead.Value);
            noeHead = noeHead.Next;
        }
        //尾到头
        noeHead = ints.Last;
        while (noeHead != null)
        {
            Debug.Log(noeHead.Value);
            noeHead = noeHead.Previous;
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_29296473/article/details/131553772