用 Rust 实现队列
对于rust-primer中实现(https://rustcc.gitbooks.io/rustprimer/content/data-structure/queue.html )的改进版本,源码如下:#[derive(Debug)]struct Queue<T> {qdata: Vec<T>,}impl <T> Queue<T>...
·
对于rust-primer中实现(https://rustcc.gitbooks.io/rustprimer/content/data-structure/queue.html )的改进版本,源码如下:
#[derive(Debug)]
struct Queue<T> {
qdata: Vec<T>,
}
impl <T> Queue<T> {
fn new() -> Self {
Queue{ qdata: Vec::new() }
}
fn push(&mut self, item: T) {
self.qdata.push(item);
}
fn pop(&mut self) ->Option<T> {
let l = self.qdata.len();
if l > 0 {
let v = self.qdata.remove(0);
Some(v)
} else {
None
}
}
}
fn main() {
let mut q = Queue::new();
q.push(1);
q.push(2);
println!("{:?}", q);
q.pop();
println!("{:?}", q);
q.pop();
println!("Hello, world!");
q.pop();
println!("Hello, world!");
}
更多推荐
已为社区贡献9条内容
所有评论(0)