C # combine two ordered linked lists into an ordered linked list

Source: Internet
Author: User

[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Text;
 
Namespace ConsoleApplication4
{
Class Program
{
Static void Display (empty list <string> ls)
{
Foreach (string s in ls)
{
Console. WriteLine (s );
}
}
Static void MergeList (sorted list <string> ls1, sorted list <string> ls2, ref sorted list <string> ls3)
{
Required listnode <string> p1 = ls1.First;
Required listnode <string> p2 = ls2.First;

Inclulistnode <string> p3 = new inclulistnode <string> ("");
If (p1.Value. CompareTo (p2.Value) <0)
{
P3.Value = p1.Value;
P1 = p1.Next;
}
Else
{
P3.Value = p2.Value;
P2 = p2.Next;
}
 
Ls3.AddFirst (p3 );
While (p1! = Null) & (p2! = Null ))
{
Required listnode <string> p4 = new required listnode <string> ("");
 
If (p1.Value. CompareTo (p2.Value) <0)
{
P4.Value = p1.Value;
Ls3.AddLast (p4 );
P1 = p1.Next;

}
Else
{
P4.Value = p2.Value;
Ls3.AddLast (p4 );
P2 = p2.Next;


}

}

While (p1! = Null)
{
Inclulistnode <string> p5 = new inclulistnode <string> ("");
P5.Value = p1.Value;
Ls3.AddLast (p5 );
P1 = p1.Next;
}
While (p2! = Null)
{
Inclulistnode <string> p6 = new inclulistnode <string> ("");
P6.Value = p2.Value;
Ls3.AddLast (p6 );
P2 = p2.Next;
}
}
Static void Main (string [] args)
{
String [] words1 = {"aa", "bb", "cc", "cd1 "};
Shortlist <string> ls1 = new shortlist <string> (words1 );
 
String [] words2 = {"aa1", "bb3", "ee", "xx "};
Shortlist <string> ls2 = new shortlist <string> (words2 );
 
Shortlist <string> ls3 = new shortlist <string> ();
MergeList (ls1, ls2, ref ls3 );
 
Display (ls3 );
Console. ReadKey ();
}
}
}
Author: weishanshan00

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.