視訊位址
頭條位址:https://www.ixigua.com/i6765442674582356483
B站位址:https://www.bilibili.com/video/av78062009?p=1
github位址
github位址
隊列介紹
隊列操作
- 初始化;
- 入隊;
- 出隊;
- 求取隊列長度。
源碼實作
#[derive(Debug)]
struct Queue<T> {
qdata: Vec<T>,
}
impl <T> Queue<T> {
fn new() -> Self {
Queue{ qdata: Vec::new() }
}
fn enqueue(&mut self, item: T) {
self.qdata.push(item);
}
fn dequeue(&mut self) ->Option<T> {
let l = self.qdata.len();
if l > 0 {
let v = self.qdata.remove(0);
Some(v)
} else {
None
}
}
fn size(&self) -> usize {
self.qdata.len()
}
}
fn main() {
let mut q = Queue::new();
q.enqueue(1);
q.enqueue(2);
println!("{:?}", q);
println!("size = {}", q.size());
q.dequeue();
println!("{:?}", q);
println!("size = {}", q.size());
q.dequeue();
println!("{:?}", q);
println!("size = {}", q.size());
q.dequeue();
println!("{:?}", q);
println!("size = {}", q.size());
}