//----------------------------------- Merging List ----------------------------------------------//
        public List Merge(List list2)
        {
            mergedList = new List("Merged List");
            List list1 = this;

            // Taking FirstNodes of the lists
            ListNode currentList1 = list1.firstNode;
            ListNode currentList2 = list2.firstNode;

            // Initializing current with 0
            int currentL1 = 0;
            int currentL2 = 0;

            // Merge list, if the list are not empty
            while (currentList1 != null &&
                   currentList2 != null)
            {
                // Getting Value of both currentLists
                currentL1 = Convert.ToInt32(currentList1.Data);
                currentL2 = Convert.ToInt32(currentList2.Data);


                // Sorting the currentNodes and
                // Adding it to the mergedList
                if (currentL1 < currentL2)
                {
                    mergedList.InsertAtBack(currentL1);
                    currentList1 = currentList1.Next;
                }
                else
                {
                    mergedList.InsertAtBack(currentL2);
                    currentList2 = currentList2.Next;
                }
            }


            // Adding remaining lists of list1 to mergedList
            while (currentList1 != null)
            {
                currentL1 = Convert.ToInt32(currentList1.Data);
                mergedList.InsertAtBack(currentL1);
                currentList1 = currentList1.Next;
            }

            // Adding remaining lists of list2 to mergedList
            while (currentList2 != null)
            {
                currentL2 = Convert.ToInt32(currentList2.Data);
                mergedList.InsertAtBack(currentL2);
                currentList2 = currentList2.Next;
            }

            return(mergedList);
        }
        static void Main(string[] args)
        {
            //List1
            List list = new List();

            list.InsertAtFront("10");
            list.InsertAtFront("50");
            list.InsertAtFront("30");
            list.InsertAtBack("45");
            list.InsertAtFront("80");
            list.InsertAtFront("20");
            list.InsertAtBack("82");

            //List2
            List list2 = new List();

            list2.InsertAtFront("52");
            list2.InsertAtFront("58");
            list2.InsertAtFront("38");
            list2.InsertAtBack("88");
            list2.InsertAtFront("85");
            list2.InsertAtFront("21");

            //Displaying Unsorted List
            Console.BackgroundColor = ConsoleColor.DarkGray;
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("\nUnsorted Lists: ");
            Console.Write("List1: ");
            list.Display();
            Console.Write("\nList2: ");
            list2.Display();

            //Sorting List
            Console.BackgroundColor = ConsoleColor.Magenta;
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("\n\nSorted Lists:");
            List SortedList1 = list.Sort();
            List SortedList2 = list2.Sort();

            Console.Write("List1: ");
            list.Display();
            Console.Write("\nList2: ");
            list2.Display();

            //Merging list
            Console.BackgroundColor = ConsoleColor.Red;
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("\n\nMerged List:");
            List mergedList = SortedList1.Merge(SortedList2);

            mergedList.Display();
            Console.ReadKey();
        }
Esempio n. 3
0
    public static void Main( string[] args )
    {
        List list = new List(); // create List container

          // create data to store in List
          bool aBoolean = true;
          char aCharacter = '$';
          int anInteger = 34567;
          string aString = "hello";

          // use List insert methods
          list.InsertAtFront( aBoolean );
          list.Display();
          list.InsertAtFront( aCharacter );
          list.Display();
          list.InsertAtBack( anInteger );
          list.Display();
          list.InsertAtBack( aString );
          list.Display();

          // use List remove methods
          object removedObject;

          // remove data from list and display after each removal
          try
          {
         removedObject = list.RemoveFromFront();
         Console.WriteLine( removedObject + " removed" );
         list.Display();

         removedObject = list.RemoveFromFront();
         Console.WriteLine( removedObject + " removed" );
         list.Display();

         removedObject = list.RemoveFromBack();
         Console.WriteLine( removedObject + " removed" );
         list.Display();

         removedObject = list.RemoveFromBack();
         Console.WriteLine( removedObject + " removed" );
         list.Display();
          } // end try
          catch ( EmptyListException emptyListException )
          {
         Console.Error.WriteLine( "\n" + emptyListException );
          } // end catch
    }
Esempio n. 4
0
 static void Main(string[] args)
 {
     LinkedListLibrary.List list = new LinkedListLibrary.List();
     Console.WriteLine("A list");
     list.InsertAtBack(2.4);
     list.InsertAtBack(4.5);
     list.InsertAtBack(2.7);
     list.InsertAtBack(1.9);
     list.InsertAtBack(7.2);
     list.Display();
     Console.WriteLine("Search for 7.3... It exists?");
     Console.WriteLine(list.Search(7.3) > 0?"Yes, it does!": "No, it does not!");
     Console.WriteLine("Search for 7.2... It exists?");
     Console.WriteLine(list.Search(7.2) > 0 ? "Yes, it does!" : "No, it does not!");
     Console.WriteLine("There are " + list.Count() + " in the List!");
     Console.WriteLine("The SUM of all elements in the list is: " + MyToString.ToString(list.Sum()));
 }