2021-10-04-《C#数据结构与算法》-学习笔记-P22-P23用无序链表实现集合与映射

本文概述了如何使用无序链表在C#中实现集合(ISet)和映射(IDictionary),包括接口定义、链表类的实现及其性能分析。通过实例演示了添加、删除和查找操作,并展示了如何将链表用于集合和字典的构建。
摘要由CSDN通过智能技术生成

总结

1.视频资源P22-P23:
https://www.bilibili.com/video/BV1gE41157pC?p=8&spm_id_from=pageDriver
2.学习内容:
1)用无序链表实现集合P22;
2)用无序链表实现映射P23。
3.2021/10/12回溯:
紫色红色笔均为回溯后增加
1)根据伪代码编写程序。

具体内容

P22 用无序链表实现集合
1、集合及集合的应用;
2、创建集合接口;
3、无序链表实现集合类;
4、集合类性能分析。

在这里插入图片描述
在这里插入图片描述

//集合接口类
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    interface ISet<E>
    {
        void Remove(E e);
        void Add(E e);
        bool Contains(E e);
        int Count{ get; }

        bool IsEmpty{ get; }
    }
}
//基于链表1类的集合类
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    class Linklist1Set<E>:ISet<E>
    {
        private Linkedlist1<E> linkedlist1set;

        public Linklist1Set()
        {
            linkedlist1set = new Linkedlist1<E>();
        }

        public int Count { get { return linkedlist1set.Count; } }

        public bool IsEmpty { get { return linkedlist1set.IsEmpty; } }

        public void Add(E e)
        {
            if (!linkedlist1set.Contains(e))
            {
                linkedlist1set.InsertFirst(e);
            }
        }

        public bool Contains(E e)
        {
            return linkedlist1set.Contains(e);
        }

        public void Remove(E e)
        {
            linkedlist1set.Remove(e);
        }
    }
}

P23 用无序链表实现映射
1、映射及映射的应用;
2、创建字典/映射接口;
3、创建支持两泛型参数链表类;
4、基于该链表类实现字典/映射类;
5、字典/映射类性能分析。

在这里插入图片描述
在这里插入图片描述

//字典接口
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    interface IDictionary<Key,Value>
    {
        void Remove(Key key);
        void Add(Key key, Value value);
        bool ContainsKey(Key key);
        Value Get(Key key);
        void Set(Key key, Value newValue);

        int Count { get; }
        bool IsEmpty { get; }
    }
}
//适用于字典的链表类Linkedlist3
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    class Linkedlist3<Key, Value>
    {
        //创建一个私有的节点类
        private class Node
        {
            public Key key;
            public Value value;
            public Node next;

            public Node(Key key, Value value, Node next)
            {
                this.key = key;
                this.value = value;
                this.next = next;
            }

            public Node(Key key, Value value)
            {
                this.key = key;
                this.next = null;
            }

            public override string ToString()
            {
                return key.ToString() + " : " + value.ToString();
            }
        }

        //记录头结点的Node型变量
        private Node head;
        //记录链表中元素个数
        private int N;

        //创建链表类的构造函数,初始化时链表没有元素
        public Linkedlist3()
        {
            head = null;
            N = 0;
        }

        private Node GetNode(Key key)
        {
            //创建cur指针,循环至索引值处
            Node cur = head;
            while (cur != null)
            {
                if (cur.key.Equals(key))
                {
                    return cur;
                }
                cur = cur.next;
            }
            return null;
        }

        #region 两个用户访问属性:链表中元素个数、链表是否为空
        public int Count
        {
            get { return N; }
        }
        public bool IsEmpty
        {
            get { return N == 0; }
        }
        #endregion

        #region 往链表中增加结点
        //往链表中增加结点--通用、首部、尾部
        public void Insert(Key key, Value value)
        {
            Node node = GetNode(key);
            //判断索引合法性
            if (node==null)
            {
                Node node1 = new Node(key, value);
                node1.next = head;
                head = node1;
                N++;
                //head = new Node(key, head);
            }

            //其他情况
            else
            {
                node.value = value;
            }
        }
        #endregion

        #region 查询、修改、包含、打印
        //查询并返回元素
        public Value Get(Key key)
        {
            Node node = GetNode(key);
            if (node == null)
            {
                throw new ArgumentException("该键不存在");
            }
            else
            {
                return node.value;
            }
        }


        //修改某键对应元素
        public void Set(Key key, Value newValue)
        {
            Node node = GetNode(key);
            if (node == null)
            {
                throw new ArgumentException("该键不存在");
            }

            else
            {
                node.value = newValue;
            }

        }

        //查询链表中是否包含某元素
        public bool ContainsKey(Key key)
        {
            return GetNode(key)!=null;
        }

        //重写ToString打印方法
        public override string ToString()
        {
            StringBuilder res = new StringBuilder();
            Node cur = head;

            for (int i = 0; i < N; i++)
            {
                res.Append(cur.key + "->");
                cur = cur.next;
            }

            res.Append("Null");
            return res.ToString();
        }
        #endregion

        # region 删除
 
        //删除指定某元素的结点
        public void Remove(Key key)
        {
            //头结点为空的情况
            if (head.next == null)
            {
                return;
            }

            //删除头部结点
            if (head.key.Equals(key))
            {
                head = head.next;
                N--;
            }

            //删除中部、尾部结点
            //两个指针 一个pre一个cur 
            else
            {
                Node pre = head;

                for (int i = 0; i < N; i++)
                {
                    pre = pre.next;
                }

                pre.next = pre.next.next;
                N--;
            }

        }
        #endregion
    }
}
//基于联表3类的字典类
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    class Linklist3Dictionary<Key,Value>:IDictionary<Key,Value>
    {
        private Linkedlist3<Key, Value> linkedlist3dictionary;

        public Linklist3Dictionary()
        {
            linkedlist3dictionary = new Linkedlist3<Key, Value>();
        }

        public int Count { get { return linkedlist3dictionary.Count; } }

        public bool IsEmpty { get { return linkedlist3dictionary.IsEmpty; } }


        public void Add(Key key, Value value)
        {
            linkedlist3dictionary.Insert(key,value);
        }

        public bool ContainsKey(Key key)
        {
            return linkedlist3dictionary.ContainsKey(key);
        }

        public Value Get(Key key)
        {
            return linkedlist3dictionary.Get(key);
        }


        public void Remove(Key key)
        {
            linkedlist3dictionary.Remove(key);
        }

        public void Set(Key key, Value newValue)
        {
            linkedlist3dictionary.Set(key,newValue);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值