CSharp · Console
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var Persons = new List<Person>();
Persons.Add(new Person("Eric"));
Persons[0].Childs = new List<Person>();
Persons[0].Childs.Add(new Person("Tom"));
Persons[0].Childs.Add(new Person("John"));
Persons[0].Childs[0].Childs = new List<Person>();
Persons[0].Childs[0].Childs.Add(new Person("Bill"));
Persons.Add(new Person("John"));
DisplayPerson(Persons);
}
public class Person
{
public Person(string name)
{
this.Name = name;
}
public string Name {get;set;}
public List<Person> Childs{get;set;}
}
public static void DisplayPerson(List<Person> persons)
{
if (persons != null)
{
Stack<Person> personStack = new Stack<Person>();
int level = 1;
persons.ForEach(personStack.Push);
while (personStack.Count > 0)
{
Person item = personStack.Pop();
Console.WriteLine("-" + item.Name + ", Level:" + level);
if (item.Childs != null)
{
item.Childs.ForEach(personStack.Push);
level++;
}
}
}
}
}