Пример #1
0
        //合成两个升序顺序表,得到一个升序顺序表
        public SeqList <int> Merge(SeqList <int> La, SeqList <int> Lb)
        {
            int           num1 = 0;
            int           num2 = 0;
            SeqList <int> Lc   = new SeqList <int>(La.GetLength() + Lb.GetLength());

            while ((num1 <= La.GetLength() - 1) && (num2 <= Lb.GetLength() - 1))
            {
                if (La[num1] < Lb[num2])
                {
                    Lc.Append(La[num1]);
                    num1++;
                }
                else
                {
                    Lc.Append(Lb[num2]);
                    num2++;
                }
            }

            while (num1 <= La.GetLength() - 1)
            {
                Lc.Append(La[num1]);
                num1++;
            }

            while (num2 <= Lb.GetLength() - 1)
            {
                Lc.Append(Lb[num2]);
                num2++;
            }

            return(Lc);
        }
Пример #2
0
        //倒置算法
        public void ReversSeqList(SeqList <T> l)
        {
            ////O(2n)
            //int len = l.GetLength();
            //T[] temp = new T[len];
            //for (int i = 0; i < len; i++)
            //{
            //    temp[i] = l[len - 1 - i];
            //}
            //for (int i = 0; i < len; i++)
            //{
            //    l[i] = temp[i];
            //}

            //O(n/2)
            T   tmp;
            int len = l.GetLength();

            for (int i = 0; i <= len / 2 - 1; ++i)
            {
                tmp            = l[i];
                l[i]           = l[len - i - 1];
                l[len - i - 1] = tmp;
            }
        }
Пример #3
0
        public SeqList <int> Purge(SeqList <int> La)
        {
            SeqList <int> Lb = new SeqList <int>(La.GetLength());

            Lb.Append(La[0]);
            for (int i = 1; i < La.GetLength(); i++)
            {
                int index = 0;
                for (int j = 0; j < Lb.Maxsize; j++)
                {
                    index = j;
                    if (La[i] == Lb[j])
                    {
                        break;
                    }
                }

                if (index >= Lb.Maxsize - 1)
                {
                    Lb.Append(La[i]);
                }
            }
            return(Lb);
        }