The code I posted above should print this out (with some cosmetic work to the print statements, and some "if exists" statements). You really should look into making this a structure like the one merlyn posted though.
Give it a shot, and reply with any errors you get.