同步操作将从 编程语言算法集/Rust 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
//! This module provides a generic `Queue` data structure, implemented using//! Rust's `LinkedList` from the standard library. The queue follows the FIFO//! (First-In-First-Out) principle, where elements are added to the back of//! the queue and removed from the front.use std::collections::LinkedList;#[derive(Debug)]pub struct Queue<T> {elements: LinkedList<T>,}impl<T> Queue<T> {// Creates a new empty Queuepub fn new() -> Queue<T> {Queue {elements: LinkedList::new(),}}// Adds an element to the back of the queuepub fn enqueue(&mut self, value: T) {self.elements.push_back(value)}// Removes and returns the front element from the queue, or None if emptypub fn dequeue(&mut self) -> Option<T> {self.elements.pop_front()}// Returns a reference to the front element of the queue, or None if emptypub fn peek_front(&self) -> Option<&T> {self.elements.front()}// Returns a reference to the back element of the queue, or None if emptypub fn peek_back(&self) -> Option<&T> {self.elements.back()}// Returns the number of elements in the queuepub fn len(&self) -> usize {self.elements.len()}// Checks if the queue is emptypub fn is_empty(&self) -> bool {self.elements.is_empty()}// Clears all elements from the queuepub fn drain(&mut self) {self.elements.clear();}}// Implementing the Default trait for Queueimpl<T> Default for Queue<T> {fn default() -> Queue<T> {Queue::new()}}#[cfg(test)]mod tests {use super::Queue;#[test]fn test_queue_functionality() {let mut queue: Queue<usize> = Queue::default();assert!(queue.is_empty());queue.enqueue(8);queue.enqueue(16);assert!(!queue.is_empty());assert_eq!(queue.len(), 2);assert_eq!(queue.peek_front(), Some(&8));assert_eq!(queue.peek_back(), Some(&16));assert_eq!(queue.dequeue(), Some(8));assert_eq!(queue.len(), 1);assert_eq!(queue.peek_front(), Some(&16));assert_eq!(queue.peek_back(), Some(&16));queue.drain();assert!(queue.is_empty());assert_eq!(queue.len(), 0);assert_eq!(queue.dequeue(), None);}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。