-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix various problems incl stack/queue bug #52
- Loading branch information
Showing
6 changed files
with
93 additions
and
39 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,45 @@ | ||
use std::collections::VecDeque; | ||
|
||
/// A one-ended queue. See also [Stack]. | ||
/// This is just a wrapper around vec so nobody pushes or pops the wrong end. | ||
pub struct Queue<T> { | ||
items: Vec<T>, | ||
items: VecDeque<T>, | ||
} | ||
|
||
impl<T> Queue<T> { | ||
pub fn new() -> Self { | ||
Queue { | ||
items: Vec::with_capacity(16), | ||
items: VecDeque::with_capacity(16), | ||
} | ||
} | ||
|
||
pub fn push(&mut self, value: T) { | ||
self.items.push(value) | ||
self.items.push_back(value) | ||
} | ||
|
||
pub fn pop(&mut self) -> Option<T> { | ||
self.items.pop() | ||
self.items.pop_front() | ||
} | ||
|
||
/// Moves all the elements from a vector into the queue. | ||
pub fn append(&mut self, mut other: Vec<T>) { | ||
self.items.append(&mut other); | ||
pub fn append(&mut self, other: Vec<T>) { | ||
for item in other.into_iter() { | ||
self.items.push_back(item); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Queue; | ||
|
||
#[test] | ||
fn test_queue() { | ||
let mut queue: Queue<i32> = Queue::new(); | ||
queue.push(1); | ||
queue.push(2); | ||
assert_eq!(1, queue.pop().unwrap()); | ||
assert_eq!(2, queue.pop().unwrap()); | ||
assert!(queue.pop().is_none()); | ||
} | ||
} |
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