美文网首页Rust
Rust队列和栈

Rust队列和栈

作者: noobHuKai | 来源:发表于2022-01-05 20:43 被阅读0次

前言

Rust 标准库里面是有 Vec这种可以在堆上分配内存的连续可增长的数组类型。可以用它很方便的实现队列和栈,但是他只能增长不能收缩。所以接下来使用链表实现队列

结点的结构

#[derive(Debug)]
struct Node<T> {
    data: T,
    next: Option<Box<Node<T>>>,
}

impl<T> Node<T> {
    fn new(data: T) -> Self {
        Node { data, next: None }
    }

    fn get_last_node(&mut self) -> &mut Self {
        if let Some(ref mut node) = self.next {
            return node.get_last_node();
        }
        self
    }
}

队列实现

采用尾插法插入结点,最后在弹出头结点。这样就实现先进先出(FIFO)


#[derive(Debug)]
struct Queue<T> {
    data: Option<Box<Node<T>>>,
    length: usize,
}

impl<T:Copy> Queue<T> {
    fn new() -> Self {
        Queue {
            data: None,
            length: 0,
        }
    }
    fn push(&mut self, data: T) {
        // push end
        if let Some(ref mut head) = self.data {
            let mut last_node = head.get_last_node();
            last_node.next = Some(Box::new(Node::new(data)));
        } else {
            self.data = Some(Box::new(Node::new(data)));
        }
        self.length += 1
    }
    fn pop(&mut self) -> Option<T> {
        if let Some(ref mut head) = self.data {
            self.length -= 1;
            let data = head.data;
            self.data = head.next.take();
            return Some(data);
        }
        None
    }
    fn length(&self) -> usize {
        self.length
    }
}

栈实现

采用头插法插入结点,最后在弹出头结点。这样就实现先进后出(FIFO)


#[derive(Debug)]
struct Stack<T> {
    data: Option<Box<Node<T>>>,
    length: usize,
}

impl<T: Copy> Stack<T> {
    fn new() -> Self {
        Stack {
            data: None,
            length: 0,
        }
    }
    fn push(&mut self, data: T) {
        let mut new_node = Node::new(data);
        // push head
        if self.data.is_some() {
            let head = self.data.take();
            new_node.next = head;
            self.data = Some(Box::new(new_node));
        } else {
            self.data = Some(Box::new(new_node));
        }
        self.length += 1
    }
    fn pop(&mut self) -> Option<T> {
        if let Some(ref mut head) = self.data {
            self.length -= 1;
            let data = head.data;
            self.data = head.next.take();
            return Some(data);
        }
        None
    }
    fn length(&self) -> usize {
        self.length
    }
}

队列代码地址

栈代码地址

相关文章

  • Rust队列和栈

    前言 Rust 标准库里面是有 Vec这种可以在堆上分配内存的连续可增长的数组类型。可以用它很方便的实现队列和栈,...

  • 数据结构——栈和队列

    用数组实现栈和队列 用栈实现队列 用队列实现栈 栈和队列的经典算法题最小间距栈宠物收养所 数组实现栈和队列 用数组...

  • 栈和队列

    用栈定义队列(出入栈) 用队列定义栈(数据队列和辅助队列)

  • Algorithm小白入门 -- 队列和栈

    队列和栈队列实现栈、栈实现队列单调栈单调队列运用栈去重 1. 队列实现栈、栈实现队列 队列是一种先进先出的数据结构...

  • 栈和队列

    栈和队列 本质上是稍加限制的线性表 栈和队列定义 栈顺序栈定义 链栈结点定义 队列顺序队列 链队列链队类型定义 链...

  • Python实现栈和队列以及使用list模拟栈和队列

    Python实现栈和队列 Python使用list模拟栈和队列

  • 算法-栈和队列算法总结

    栈和队列算法总结 1 模拟 1.1 使用栈实现队列 1.2 使用队列实现栈 2 栈的应用 2.1 栈操作 2.2 ...

  • 算法分析 [BFS、Greedy贪心] 2019-02-18

    队列 和 栈 232. 用栈实现队列 Implement Queue using Stacks双栈,出队列时,将i...

  • 实 验 四 栈和队列

    一、实验目的与要求:## 1、理解栈和队列抽象数据类型。 2、掌握栈和队列的存储结构和操作实现。 3、理解栈和队列...

  • 栈、队列和链表

    基本数据结构 栈和队列 栈和队列都是动态集合。栈实现的是一种后进先出策略。队列是一种先进先出策略。 栈 栈上的in...

网友评论

    本文标题:Rust队列和栈

    本文链接:https://www.haomeiwen.com/subject/frpkcrtx.html