顺序查找,C#算法实现

SeqList类:

using System;

namespace seqlist.List {
    public class SeqList  {
        private T[] element;
        //顺序表的数组容量
        private int size;
        //顺序表的长度
        private int length;

        public SeqList(int size) {
            this.size = size < 64?64:size;
            this.element = new T[this.size];
            this.length = 0;
        }

        public SeqList(T[] value){
            int n = value.Length;
            if(n>0) {
                this.size = 2*n;
                this.element = new T[this.size];
                for(int i=0;i=0 && i=0 && ilength) i = length;
            for(int j=length-1;j>=i;j--){
                element[j+1] = element[j];
            }
            element[i] = x;
            length ++;
        }

        public void insert(T x) {
            this.insert(length,x);
        }


        public bool remove(int i, ref T old) {
            if(length > 0 && i>=0 && i= 0;
        }
        public void clear() {
            this.length = 0;
        }
    }
}

主程序类:

using System;
using seqlist.List;

namespace seqlist
{
    class Program
    {
        static void Main(string[] args)
        {
            SeqList sList = new SeqList(64);
            sList.insert(1);
            sList.insert(2);
            sList.insert(3);
            sList.insert(4);
            Console.WriteLine(sList.contains(3));
        }
    }
}

程序输出:


1.png

你可能感兴趣的:(顺序查找,C#算法实现)