A recursive example

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Collections;
Namespace SortApplication
{
Public class getList
{
Public int id;
Public int parentId;
Public string name;
Public getList (int id, int parentId, string name)
{
This. id = id;
This. parentId = parentId;
This. name = name;
}
}
Class Program
{
Private static IList <getList> GetList (getList [] myList, int pid)
{
Int count = myList. Length;
IList <getList> al = new List <getList> ();
For (int I = 0; I <count; I ++)
{
If (myList [I]. parentId = pid)
{
// Console. WriteLine ("test above:" + pid );
Al. Add (myList [I]);
}
}
Return al;
}
Private static void SortList (getList [] myList, IList <getList> Al, ref getList [] listCopy)
{
If (listCopy [0] = null)
{
Al. Add (myList [0]);
}
Try
{
Foreach (getList lis in Al)
{
Int I;
For (I = 0; I <listCopy. Length; I ++)
{
If (listCopy [I] = null)
{
Break;
}
}
// Console. WriteLine ("//" + I + "//////");
ListCopy [I] = (getList) lis;
// Console. WriteLine (f );
Al = GetList (myList, lis. id );
SortList (myList, Al, ref listCopy );

}
}
Catch (Exception ex)
{
Console. WriteLine (ex. Message );
}
}
Static void Main (string [] args)
{
GetList [] ourList = new getList [] {
New getList (1, 0, ""),
New getList (2, 1, "B "),
New getList (3, 1, "c "),
New getList (4, 2, "d "),
New getList (5, 4, "e "),
New getList (7, 3, "aaa "),
New getList (6, 5, "f ")
};

String BlankBase = new string ('', 8 );
Console. WriteLine ("sorting previous objects ");
For (int I = 0; I <ourList. Length; I ++)
{
Console. WriteLine (ourList [I]. id + BlankBase + ourList [I]. parentId + BlankBase + ourList [I]. name );
}
GetList [] listCopy = new getList [ourList. Length];
IList <getList> Alist = new List <getList> ();
SortList (ourList, Alist, ref listCopy );
Console. WriteLine ("objects sorted by \ r \ n ");
For (int I = 0; I <listCopy. Length; I ++)
{
Try
{
Console. WriteLine (listCopy [I]. id + BlankBase + listCopy [I]. parentId + BlankBase + listCopy [I]. name );
}
Catch (Exception ex)
{
Console. WriteLine (ex. Message );
}
}
}
}
}


From the wangganggang90 Column

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.