Ejemplo n.º 1
0
        /// <summary>
        /// 倒置顺序表
        /// </summary>
        /// <param name="la">顺序表la</param>
        /// <param name="lb">顺序表lb</param>
        static SequenceList <int?> MergeListPro(SequenceList <int?> la, SequenceList <int?> lb)
        {
            SequenceList <int?> lc = new SequenceList <int?>(la.MaxSize + lb.MaxSize);

            int a = 0, b = 0;

            //注意:一次循环插入la,lb中各一个数据
            while (a < la.GetLength() && b < lb.GetLength())
            {
                if (la[a] < lb[b])
                {
                    lc.Append(la[a++]);
                }
                else
                {
                    lc.Append(lb[b++]);
                }
            }

            //la 没有循环完毕,把剩余的la添加到lc中
            while (a < la.GetLength())
            {
                lc.Append(la[a++]);
            }

            //lb 没有循环完毕,把剩余的lb添加到lc中
            while (b < lb.GetLength())
            {
                lc.Append(lb[b++]);
            }

            return(lc);
        }
Ejemplo n.º 2
0
        private static void MergeListProTest()
        {
            Console.WriteLine();

            Console.WriteLine(Hbb0b0.Trace.FunctionCallHelper.GetCurrentCallFunctionName(null));

            Console.WriteLine();

            SequenceList <int?> La = new SequenceList <int?>(3);

            La.Append(1);
            La.Append(5);
            La.Append(8);

            SequenceList <int?> Lb = new SequenceList <int?>(5);

            Lb.Append(1);
            Lb.Append(2);
            Lb.Append(7);
            Lb.Append(8);
            Lb.Append(9);

            Console.WriteLine("----Input data begin-----");
            Show <int?>(La);
            Console.WriteLine();
            Show <int?>(Lb);
            Console.WriteLine();
            Console.WriteLine("---End-----");

            SequenceList <int?> result = MergeListPro(La, Lb);

            Show <int?>(result);
        }
Ejemplo n.º 3
0
        private static void PurgeListTest()
        {
            Console.WriteLine();
            Console.WriteLine(Hbb0b0.Trace.FunctionCallHelper.GetCurrentCallFunctionName(null));
            Console.WriteLine();

            SequenceList <int> purgeList = new SequenceList <int>(20);

            purgeList.Append(1);
            purgeList.Append(9);
            purgeList.Append(7);
            purgeList.Append(3);
            purgeList.Append(2);
            purgeList.Append(1);
            purgeList.Append(3);
            purgeList.Append(4);
            purgeList.Append(3);

            Console.WriteLine("----Input data begin-----");
            Show <int>(purgeList);
            Console.WriteLine();
            Console.WriteLine("---End-----");

            SequenceList <int> result = PurgeList(purgeList);

            Show <int>(result);
        }
        /// <summary>
        ///Delete 的测试
        ///</summary>
        public void DeleteTestHelperString()
        {
            int size = 10;                                                  // TODO: 初始化为适当的值
            SequenceList <String> target = new SequenceList <string>(size); // TODO: 初始化为适当的值

            List <string> list = new List <string>()
            {
                "0", "1", "2", "3", "4", "5"
            };

            foreach (var item in list)
            {
                target.Append(item);
            }

            target.Delete(0);

            target.Delete(2);

            target.Delete(3);

            Assert.AreEqual(3, target.GetLength());
            Assert.AreEqual("1", target[0]);
            Assert.AreEqual("2", target[1]);
            Assert.AreEqual("4", target[2]);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 去除顺序表的重复项
        /// </summary>
        /// <param name="la"></param>
        static SequenceList <int> PurgeList(SequenceList <int> la)
        {
            SequenceList <int> myList = new SequenceList <int>(la.MaxSize);

            int  j     = 0;
            bool ifHas = false;

            for (int i = 0; i < la.GetLength(); i++)
            {
                j     = 0;
                ifHas = false;
                while (j < la.GetLength())
                {
                    if (i != j && la[i] == la[j])
                    {
                        ifHas = true;
                        break;
                    }
                    j++;
                }
                if (ifHas == false)
                {
                    myList.Append(la[i]);
                }
            }

            return(myList);
        }
        /// <summary>
        ///GetLength 的测试
        ///</summary>
        public void GetLengthTestHelper <T>()
        {
            int size = 5;                                         // TODO: 初始化为适当的值
            SequenceList <T> target = new SequenceList <T>(size); // TODO: 初始化为适当的值
            int expected            = 0;                          // TODO: 初始化为适当的值
            int actual;

            actual = target.GetLength();
            Assert.AreEqual(expected, actual);

            target.Append(default(T));
            Assert.AreEqual(1, target.GetLength());

            target.Append(default(T));
            Assert.AreEqual(2, target.GetLength());
        }
        /// <summary>
        ///Last 的测试
        ///</summary>
        public void LastTestHelperString()
        {
            int size = 3;                                                   // TODO: 初始化为适当的值
            SequenceList <string> target = new SequenceList <string>(size); // TODO: 初始化为适当的值
            int expected = -1;                                              // TODO: 初始化为适当的值
            int actual;

            actual = target.Last;
            Assert.AreEqual(expected, actual);

            target.Append("a");
            Assert.AreEqual(0, target.Last);

            target.Append("b");
            Assert.AreEqual(1, target.Last);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 合并顺序表
        /// </summary>
        /// <param name="la"></param>
        /// <param name="lb"></param>
        static SequenceList <int?> MergeList(SequenceList <int?> la, SequenceList <int?> lb)
        {
            //合并后的表长度 length=la.MaxSize+lb.MaxSize-1
            int length             = la.MaxSize + lb.MaxSize;
            SequenceList <int?> lc = new SequenceList <int?>(length);


            //复制La到myList
            for (int i = 0; i < la.GetLength(); i++)
            {
                lc.Append(la[i]);
            }

            //Lb中的值与myList比较,如果当前值Lb[i]比myList的某个位置的值相等或等于,
            //那么lb[i]插入到mylist对应的位置中,否则lb[i]放到mylist的末尾。
            for (int i = 0; i < lb.GetLength(); i++)
            {
                //是否找到lb[i]<=mylist[j]的标志
                bool find = false;

                //开始查找位置
                int startLocation = 0;

                for (int j = startLocation; j < length; j++)
                {
                    if (lc[j].HasValue && lb[i] <= lc[j])
                    {
                        find = true;
                        lc.Insert(lb[i], j);
                        startLocation = j;
                        break;
                    }
                }
                //如果找不到
                if (!find)
                {
                    lc.Append(lb[i]);
                }
            }

            return(lc);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 倒置顺序表
        /// </summary>
        /// <param name="sqList"></param>
        static SequenceList <int> ReverseList(SequenceList <int> sqList)
        {
            SequenceList <int> myList = new SequenceList <int>(sqList.MaxSize);
            int length = sqList.GetLength();

            for (int i = 1; i <= length; i++)
            {
                myList.Append(sqList[length - i]);
            }
            return(myList);
        }
        /// <summary>
        ///InsertAfter 的测试
        ///</summary>
        public void InsertTestHelperString()
        {
            int size = 10;                                                  // TODO: 初始化为适当的值
            SequenceList <string> target = new SequenceList <string>(size); // TODO: 初始化为适当的值

            try
            {
                target.Insert("A", -1);
            }
            catch (Exception myEx)
            {
                Assert.IsInstanceOfType(myEx, typeof(DataStructureException));
            }

            try
            {
                target.Insert("A", 10);
            }
            catch (Exception myEx)
            {
                Assert.IsInstanceOfType(myEx, typeof(DataStructureException));
            }

            target.Append("a");
            target.Append("d");
            target.Append("f");

            target.Insert("b", 0);
            target.Insert("c", 1);
            target.Insert("x", 4);

            List <string> list = new List <string>()
            {
                "b", "c", "a", "d", "x", "f"
            };

            for (int i = 0; i < list.Count; i++)
            {
                Assert.AreEqual(list[i], target[i]);
            }
        }
        /// <summary>
        ///Locate 的测试
        ///</summary>
        public void LocateTestHelperString()
        {
            int size = 10;                                                  // TODO: 初始化为适当的值
            SequenceList <string> target = new SequenceList <string>(size); // TODO: 初始化为适当的值

            string str1 = "AAA";
            string str2 = "aaa";

            target.Append(str1);
            target.Append(str2);
            target.Append(str2);



            Assert.AreEqual(0, target.Locate(str1));
            Assert.AreEqual(1, target.Locate(str2));

            Assert.AreEqual(1, target.Locate("aaa"));

            Assert.AreEqual(-1, target.Locate("a"));
        }
        /// <summary>
        ///IsEmpty 的测试
        ///</summary>
        public void IsEmptyTestHelper <T>()
        {
            int size = 1;                                           // TODO: 初始化为适当的值
            SequenceList <T> target   = new SequenceList <T>(size); // TODO: 初始化为适当的值
            bool             expected = true;                       // TODO: 初始化为适当的值
            bool             actual;

            actual = target.IsEmpty();
            Assert.AreEqual(expected, actual);

            target.Append(default(T));

            Assert.AreEqual(false, target.IsEmpty());
        }
        /// <summary>
        ///Append 的测试
        ///</summary>
        public void AppendTestHelper <T>()
        {
            int size = 0;                                         // TODO: 初始化为适当的值
            SequenceList <T> target = new SequenceList <T>(size); // TODO: 初始化为适当的值
            T node = default(T);                                  // TODO: 初始化为适当的值

            try
            {
                target.Append(node);
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(DataStructureException));
            }
        }
        /// <summary>
        ///Clear 的测试
        ///</summary>
        public void ClearTestHelper <T>()
        {
            int size = 10;                                        // TODO: 初始化为适当的值
            SequenceList <T> target = new SequenceList <T>(size); // TODO: 初始化为适当的值


            target.Append(default(T));

            target.Clear();

            Assert.AreEqual(-1, target.Last);

            Assert.AreEqual(0, target.GetLength());

            //Assert.IsNull(target.Data);
        }
        public void IsFullTestHelperInt()
        {
            int size = 5;                                             // TODO: 初始化为适当的值
            SequenceList <int> target = new SequenceList <int>(size); // TODO: 初始化为适当的值
            bool expected             = false;                        // TODO: 初始化为适当的值
            bool actual;

            actual = target.IsFull();
            Assert.AreEqual(expected, actual);

            for (int i = 0; i < size; i++)
            {
                target.Append(i);
            }

            Assert.IsTrue(target.IsFull());
        }
Ejemplo n.º 16
0
        private static void ReverseListTest()
        {
            Console.WriteLine();
            Console.WriteLine(Hbb0b0.Trace.FunctionCallHelper.GetCurrentCallFunctionName(null));
            Console.WriteLine();

            SequenceList <int> sqList = new SequenceList <int>(10);

            for (int i = 0; i < 10; i++)
            {
                sqList.Append(i);
            }

            Console.WriteLine("----Input data begin-----");
            Show <int>(sqList);
            Console.WriteLine();
            Console.WriteLine("---End-----");

            SequenceList <int> result = ReverseList(sqList);

            Show <int>(result);
        }
        /// <summary>
        ///GetElement 的测试
        ///</summary>
        public void GetElementTestHelperString()
        {
            int size = 4;                                                   // TODO: 初始化为适当的值
            SequenceList <string> target = new SequenceList <string>(size); // TODO: 初始化为适当的值

            List <string> mylist = new List <string>()
            {
                "1", "2", "3", "4"
            };

            foreach (string item in mylist)
            {
                target.Append(item);
            }

            for (int i = 0; i < 4; i++)
            {
                Assert.AreEqual(mylist[i], target.GetElement(i));
            }

            Assert.IsNull(target.GetElement(10));
            Assert.IsNull(target.GetElement(-100));
        }
        public void AppendTestHelperString()
        {
            int size = 5;                                                   // TODO: 初始化为适当的值
            SequenceList <string> target = new SequenceList <string>(size); // TODO: 初始化为适当的值

            List <string> myList = new List <string>()
            {
                "a", "b", "c", "d"
            };



            foreach (string item in myList)
            {
                target.Append(item);
            }

            Assert.AreEqual(myList.Count, target.GetLength());

            for (int i = 0; i < myList.Count; i++)
            {
                Assert.AreEqual(myList[i], target[i]);
            }
        }