import java.util.*;
public class dfs implements VisitListener{
public void onVisit(node n) {
// Do nothing
System.out.println(n.getId());
}
public Stack q = new Stack();
public ArrayList vn = new ArrayList();
public ArrayList search(node startNode,VisitListener search) {
this.q.clear();
this.vn.clear();
this.q.add(startNode);
this.vn.add(startNode);
node cur;
while(!(q.isEmpty())) {
cur = this.q.pop();
if(cur.isvisited()) {
continue;
}
cur.visit();
search.onVisit(cur);
Collections.reverse(cur.connectedNodes);
for(node sub:cur.connectedNodes) {
q.add(sub);
vn.add(sub);
}
}
return vn;
}
public ArrayList search(node startNode) {
return this.search(startNode, this);
}
public static void main(String[] args) {
node a,b,c,d,e,f,g;
a = (new node()).setid(1);
b = (new node()).setid(2);
c = (new node()).setid(3);
a.linkNode(b);
a.linkNode(c);
d = (new node()).setid(4);
e = (new node()).setid(5);
f = (new node()).setid(6);
g = (new node()).setid(7);
b.linkNode(d);
b.linkNode(e);
c.linkNode(f);
c.linkNode(g);
(new dfs()).search(a);
}
}