/* queue This question requires you to use queues to implement the functionality of the stac */ // I AM NOT DONE #[derive(Debug)] pub struct Queue { elements: Vec, } impl Queue { pub fn new() -> Queue { Queue { elements: Vec::new(), } } pub fn enqueue(&mut self, value: T) { self.elements.push(value) } pub fn dequeue(&mut self) -> Result { if !self.elements.is_empty() { Ok(self.elements.remove(0usize)) } else { Err("Queue is empty") } } pub fn peek(&self) -> Result<&T, &str> { match self.elements.first() { Some(value) => Ok(value), None => Err("Queue is empty"), } } pub fn size(&self) -> usize { self.elements.len() } pub fn is_empty(&self) -> bool { self.elements.is_empty() } } impl Default for Queue { fn default() -> Queue { Queue { elements: Vec::new(), } } } pub struct myStack { //TODO q1:Queue, q2:Queue } impl myStack { pub fn new() -> Self { Self { //TODO q1:Queue::::new(), q2:Queue::::new() } } pub fn push(&mut self, elem: T) { //TODO } pub fn pop(&mut self) -> Result { //TODO Err("Stack is empty") } pub fn is_empty(&self) -> bool { //TODO true } } #[cfg(test)] mod tests { use super::*; #[test] fn test_queue(){ let mut s = myStack::::new(); assert_eq!(s.pop(), Err("Stack is empty")); s.push(1); s.push(2); s.push(3); assert_eq!(s.pop(), Ok(3)); assert_eq!(s.pop(), Ok(2)); s.push(4); s.push(5); assert_eq!(s.is_empty(), false); assert_eq!(s.pop(), Ok(5)); assert_eq!(s.pop(), Ok(4)); assert_eq!(s.pop(), Ok(1)); assert_eq!(s.pop(), Err("Stack is empty")); assert_eq!(s.is_empty(), true); } }