【Unity 17】C# 栈,泛型, 线性表的定义与使用

22 篇文章 9 订阅

PS:本系列笔记将会记录我此次在北京学习Unity开发的总体过程,方便后期写总结,笔记为日更。
笔记内容均为 自己理解,不保证每个都对。
C#笔记未按照难度排列

Part 1 栈:先进后出

首先定义接口:

    public interface Istack<int>     //接口默认为public
    {
        int PopStack();		//出栈

        void EnStack(T tmpInt);		//进栈

        bool IsFull();		//判空
    }

接口的实现

      public class ICStack : Istack		//遵守Istack的接口
    {
        int[] myArray;
        int cnt = 0;

        public ICStack(int Number)
        {
            myArray = new int[Number];
            cnt = 0;
        }

        public int PopStack()       //出栈
        {
            if (cnt != 0)
            {
                cnt--;
                int result = myArray[cnt];
                return result;
            }
            else                    //若栈空返回int的最大值
                return int.MaxValue;
        }

        public void EnStack(int tmpInt)     //进栈
        {
            if (!IsFull())      //判断栈是否满了
            {
                myArray[cnt++] = tmpInt;
            }
            else
            {
                Console.WriteLine("Stack is Full!!!" + cnt);
            }



        }

        public bool IsFull()        //判满
        {
            if (cnt == myArray.Length)
                return true;
            else
                return false;
        }

        public void ShowMe()		//展示
        {

            for (int i = 0; i <= cnt - 1; i++)
            {
                Console.WriteLine("myArray[{0}] == {1}", i, myArray[i]);
            }
        }
    }

使用:

            ICStack tmpICstack = new ICStack(3);		//设置栈长为3
            int result = tmpICstack.PopStack();		//栈空时出栈建测
            Console.WriteLine("栈空出栈" + result);

            tmpICstack.EnStack(4);		//4进栈
            tmpICstack.EnStack(5);		//5进栈
            tmpICstack.EnStack(6);		//6进栈

            tmpICstack.ShowMe();		//输出栈

            Console.WriteLine("=======================================");

            tmpICstack.PopStack();		//6出栈
            tmpICstack.EnStack(16);		//16进栈
            tmpICstack.ShowMe();		//展示

Part 2 泛型

定义: : 表示 对类里面的 东西 发生替换。
在作用域范围内,T可以是任意值,任意类型
例如:利用泛型定义接口

    public interface Istack<T>     //接口默认为public
    {
        T PopStack();

        void EnStack(T tmpInt);

        bool IsFull();
    }

实现接口:

    public class ICStack<T>:Istack<T>       //泛型演示 T适用于整个Class
    {
        //泛型
        //泛化:
        //Class A<T> {}     <T>表示引入一个T类型的泛化,作用域为{   }内
        //方法参数泛化
        //回调参数泛化

         

        T[] myArray;
        int cnt = 0;

        public ICStack(int Number)
        {
            myArray = new T[Number];
            cnt = 0;
        }

        public T PopStack()       //出栈
        {
            if (cnt != 0)
            {
                cnt--;
                T result = myArray[cnt];
                return result;
            }
            else                    //若栈空返回int的最大值
                return default(T);      //default(T)表示T的初始值     值类型一般为0  引用类型一般为null
        }

        public void EnStack(T tmpInt)     //进栈
        {
            if (!IsFull())      //判断栈是否满了
            {
                myArray[cnt++] = tmpInt;
            }
            else
            {
                Console.WriteLine("Stack is Full!!!" + cnt);
            }



        }

        public bool IsFull()        //判满
        {
            if (cnt == myArray.Length)
                return true;
            else
                return false;
        }

        public void ShowMe()
        {

            for (int i = 0; i <= cnt - 1; i++)
            {
                Console.WriteLine("myArray[{0}] == {1}", i, myArray[i]);
            }
        }
    }

使用:

            ICStack<int> tmpStack = new ICStack<int>(3);		//先申明T的类型

            tmpStack.EnStack(4);
            tmpStack.EnStack(5);
            tmpStack.ShowMe();

六种约束类型:
T:结构struct 类型参数必须是值类型。可以指定除 Nullable 以外的任何值类型。有关更多信息,请参见使用可空类型(C# 编程指南)。
T:类 class 类型参数必须是引用类型,包括任何类、接口、委托或数组类型。
T:new() 类型参数必须具有无参数的公共构造函数。当与其他约束一起使用时,new() 约束必须最后指定。
T:<基类名> 类型参数必须是指定的基类或派生自指定的基类。
T:<接口名称> 类型参数必须是指定的接口或实现指定的接口。可以指定多个接口约束。约束接口也可以是泛型的。
T:U 两个同级别的类也可以 为 T 提供的类型参数必须是为 U 提供的参数或派生自为 U 提供的参数。这称为裸类型约束。

两个约束之间 不用加标点:

       public void Add<T, U>(T t, U u)    where T : TestA  where U : TestA
        {
            TestA test = t + u;
            Console.WriteLine(test.ToString());
        }

对一个泛型 遵守多个约束 之间 用逗号隔开 :

T : Women ,new()

Part 3 线性表:

定义接口:

    public interface ListInterFace
    {
        void Append(int tmpInt);			//增加

        void Inert(int index , int tmpInt);		//插入

        void Delete(int tmpInt);		//删除

        void Update(int index, int tmpInt);		//修改

        int Find(int tmpInt);		//查询

    }

实现接口:

    class SeqList:ListInterFace
    {

        int[] myArray;
        public SeqList(int Number)
        {
            myArray = new int[Number];
        }
        int cnt = -1;   //表示当前下标值    


       public void Append(int tmpInt)
        {
            if (!IsFull())
            {
                cnt++;
                myArray[cnt] = tmpInt;
            }
        }

       public void Inert(int index, int tmpInt)
        {
            if (IsFull())       //数组已满
                return;

            if (index >= cnt || cnt == -1)        //插入位置大于当前数组长度    或者数组中没有任何元素
            {
                cnt++;
                myArray[cnt] = tmpInt;
            }

            //数组后移
            for (int i = myArray.Length - 2; i >= index; i--)
            {
                myArray[i + 1] = myArray[i];
            }
            myArray[index] = tmpInt;
        }

       public void Delete( int tmpInt)
        {
            int index = Find(tmpInt);
            if(index != -1  )       //表示找到
            {   //找到后,所有元素前移
                for (int i = index; i < cnt; i++)
                {
                    myArray[i] = myArray[i + 1];
                }
                cnt--;

            }
        }

       public void Update(int index, int tmpInt)
        {
            if (index < 0 || index > cnt)    //超出界限
            {
                return;
            }
            else;
            myArray[index] = tmpInt;
        }

       public int Find(int tmpInt)
        {
            for (int i = 0; i <= cnt; i++)
            {
                if(myArray[i] == tmpInt)
                {
                    return 1;       //找到
                }
                else
                {
                    return -1;      //未找到
                }
            }
            return 0;
        }

        public bool IsFull()    //判断数组是否为满
        {
            if (cnt >= myArray.Length - 1)
                return true;
            else
                return false;
                    
        }

        public void ShowMe()
        {
            for (int i = 0; i < myArray.Length; i++)
            {
                Console.WriteLine("myArray[{0}] == {1}", i, myArray[i]);
            }
        }
    }
}

线性表的使用:

            SeqList tmpList = new SeqList(5);		//设定线性表的长度为5
            tmpList.Append(1);		//1进入
            tmpList.Append(2);		//2进入
            tmpList.Append(3);		//3进入
            tmpList.Append(5);		//4进入
            tmpList.Update(3, 10);		//修改下标为3的值改为10
            tmpList.Inert(2, 7);			//在下标为2的地方插入7
            tmpList.Delete(7);			//删除值为7的下标

            tmpList.ShowMe();		//展示
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值