using System; using System.Collections.Generic; using System.Linq; using System.Text; using ConsoleApplication3; namespace MergeList { /*Sorted list merge class*/ class Program { static public void Merge(SeqList<int> La, SeqList<int> Lb) { SeqList<int> Lc = new SeqList<int>(La.Maxsize + Lb.Maxsize); int i = 0; int j = 0; //两个表中都有数据元素 while ((i <= (La.GetLength() - 1)) && (j <= (Lb.GetLength() - 1))) { if (La[i] < Lb[j]) { Lc.Append(La[i++]); } else { Lc.Append(Lb[j++]); } } //a表中还有数据元素 while (i <= (La.GetLength() - 1)) { Lc.Append(La[i++]); } //b表中还有数据元素 while (j <= (Lb.GetLength() - 1)) { Lc.Append(Lb[j++]); } Console.WriteLine("The length of Merged list is {0}", Lc.GetLength()); for (int k = 0; k <= Lc.Last; k++) { Console.Write("{0} ", Lc[k]); } } //static public void MergeList(SeqList<int> A, SeqList<int> B) //{ // SeqList<int> C = new SeqList<int>(A.GetLength()+B.GetLength()); // int j = 0, i = 0; // if (A.GetLength() - 1 <= B.GetLength() - 1) // { // while ((j <= B.GetLength() - 1) && (i <= A.GetLength() - 1)) // { // if (A[i] < B[j]) // { // C.Append(A[i++]); // } // //else if (A[i] == B[j]) // //{ // // C.Append(A[i++]); // // C.Append(B[j++]); // //} // else // C.Append(B[j++]); // } // //There are still some elements in B // while (j < B.GetLength() - 1) // { // C.Append(B[j++]); // } // } // else // { // while (i < A.GetLength() - 1) // C.Append(A[i++]); // } // Console.WriteLine("The length of Merged list is {0}", C.GetLength()); // for (int k = 0; k <= C.Last; k++) // { // Console.Write("{0} ", C[k]); // } //} static void Main(string[] args) { #region //Initialize two seq lists L1 and L2. SeqList<int> L1 = new SeqList<int>(3); SeqList<int> L2 = new SeqList<int>(4); //Add elements on L1 L1.Append(2); for (int i = 1; i <= L1.Maxsize-1; i++) { L1.Append(i * 2); } //Add elements on L2 for (int k = 0; k <= L2.Maxsize - 1; k++) { L2.Append(k * 3); } Console.WriteLine("The length of L1 is {0}", L1.GetLength()); //Present each element in L1 for (int j = 0; j <= L1.Last; j++) { Console.Write("{0} ", L1[j]); } Console.WriteLine(); //Present each element in L1 Console.WriteLine("The length of L2 is {0}", L2.GetLength()); for (int j = 0; j <= L2.Last; j++) { Console.Write("{0} ", L2[j]); } #endregion Merge(L1, L2); Console.ReadKey(); } } }