Created
December 12, 2014 15:00
-
-
Save 46bit/f96bac392c8bb21a8e46 to your computer and use it in GitHub Desktop.
What pointers do I need to add to make this work? Edges should store references to nodes; nodes store references to edges.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Node<D> { | |
edges: Vec<Edge<D>>, | |
pub data: D | |
} | |
impl<D> Node<D> { | |
pub fn new(data: D) -> Node<D> { | |
Node{data: data, edges: Vec::new()} | |
} | |
} | |
struct Edge<D> { | |
nodes: [Node<D>, .. 2], | |
pub cost: f64 | |
} | |
impl<D> Edge<D> { | |
pub fn new(node_a: & mut Node<D>, node_b: & mut Node<D>, cost: f64) -> Edge<D> { | |
let new_edge = Edge{nodes: [node_a, node_b], cost: cost}; | |
node_a.edges.push(new_edge); | |
node_b.edges.push(new_edge); | |
new_edge | |
} | |
} | |
struct NodeInfo { | |
x: u32, | |
y: u32, | |
symbol: char, | |
symbol_cost: f32 | |
} | |
fn main() { | |
let mut a = Node::new("Alpha"); | |
let mut b = Node::new("Beta"); | |
let mut c = Node::new("Gamma"); | |
println!("{}, {}, {}", a.data, b.data, c.data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment