(graph: Vec<(Graph, usize)>)
| 82 | } |
| 83 | |
| 84 | fn dfs(graph: Vec<(Graph, usize)>) { |
| 85 | let mut gp = graph; |
| 86 | let mut nodes: Vec<usize> = Vec::new(); |
| 87 | let mut temp: Vec<usize> = Vec::new(); |
| 88 | |
| 89 | gp[1].1 = 1; |
| 90 | let mut curr = gp[1].0.get_first().clone(); |
| 91 | |
| 92 | // 打印图 |
| 93 | print!("{}->",1); |
| 94 | while let Some(val) = curr { |
| 95 | nodes.insert(0, val.borrow().data); |
| 96 | curr = val.borrow().next.clone(); |
| 97 | } |
| 98 | |
| 99 | // 打印深度优先图 |
| 100 | loop{ |
| 101 | if 0 == nodes.len() { |
| 102 | break; |
| 103 | }else{ |
| 104 | let data = nodes.pop().unwrap(); |
| 105 | if 0 == gp[data].1 { |
| 106 | gp[data].1 = 1; |
| 107 | print!("{data}->"); |
| 108 | |
| 109 | // 节点加入 temp |
| 110 | let mut curr = gp[data].0.get_first().clone(); |
| 111 | while let Some(val) = curr { |
| 112 | temp.push(val.borrow().data); |
| 113 | curr = val.borrow().next.clone(); |
| 114 | } |
| 115 | |
| 116 | while !temp.is_empty(){ |
| 117 | nodes.push(temp.pop().unwrap()); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | println!(""); |
| 123 | } |
| 124 | |
| 125 | fn main() { |
| 126 | let data = [[1,2],[2,1],[1,3],[3,1],[2,4],[4,2],[2,5],[5,2],[3,6],[6,3],[3,7],[7,3],[4,5],[5,4],[6,7],[7,6],[5,8],[8,5],[6,8],[8,6]]; |