Skip to content

Commit 9371380

Browse files
committed
feat: add rust codes for linkedlist_queue
1 parent a375ad6 commit 9371380

File tree

4 files changed

+131
-7
lines changed

4 files changed

+131
-7
lines changed

rust/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ path = "chapter_stack_and_queue/stack.rs"
5454
name = "linkedlist_stack"
5555
path = "chapter_stack_and_queue/linkedlist_stack.rs"
5656

57+
# Run Command: cargo run --bin linkedlist_queue
58+
[[bin]]
59+
name = "linkedlist_queue"
60+
path = "chapter_stack_and_queue/linkedlist_queue.rs"
61+
5762
# Run Command: cargo run --bin queue
5863
[[bin]]
5964
name = "queue"
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* File: linkedlist_queue.rs
3+
* Created Time: 2023-03-11
4+
* Author: sjinzh (sjinzh@gmail.com)
5+
*/
6+
7+
include!("../include/include.rs");
8+
9+
use std::rc::Rc;
10+
use std::cell::RefCell;
11+
12+
/* 基于链表实现的队列 */
13+
#[allow(dead_code)]
14+
pub struct LinkedListQueue<T> {
15+
front: Option<Rc<RefCell<ListNode<T>>>>, // 头结点 front
16+
rear: Option<Rc<RefCell<ListNode<T>>>>, // 尾结点 rear
17+
que_size: usize, // 队列的长度
18+
}
19+
20+
impl<T: Copy> LinkedListQueue<T> {
21+
pub fn new() -> Self {
22+
Self {
23+
front: None,
24+
rear: None,
25+
que_size: 0,
26+
}
27+
}
28+
29+
/* 获取队列的长度 */
30+
pub fn size(&self) -> usize {
31+
return self.que_size;
32+
}
33+
34+
/* 判断队列是否为空 */
35+
pub fn is_empty(&self) -> bool {
36+
return self.size() == 0;
37+
}
38+
39+
/* 入队 */
40+
pub fn push(&mut self, num: T) {
41+
// 尾结点后添加 num
42+
let new_rear = ListNode::new(num);
43+
match self.rear.take() {
44+
// 如果队列不为空,则将该结点添加到尾结点后
45+
Some(old_rear) => {
46+
old_rear.borrow_mut().next = Some(new_rear.clone());
47+
self.rear = Some(new_rear);
48+
}
49+
// 如果队列为空,则令头、尾结点都指向该结点
50+
None => {
51+
self.front = Some(new_rear.clone());
52+
self.rear = Some(new_rear);
53+
}
54+
}
55+
self.que_size += 1;
56+
}
57+
58+
/* 出队 */
59+
pub fn poll(&mut self) -> Option<T> {
60+
self.front.take().map(|old_front| {
61+
match old_front.borrow_mut().next.take() {
62+
Some(new_front) => {
63+
self.front = Some(new_front);
64+
}
65+
None => {
66+
self.rear.take();
67+
}
68+
}
69+
self.que_size -= 1;
70+
Rc::try_unwrap(old_front).ok().unwrap().into_inner().val
71+
})
72+
}
73+
74+
/* 访问队首元素 */
75+
pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {
76+
self.front.as_ref()
77+
}
78+
79+
/* 将链表转化为 Array 并返回 */
80+
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
81+
if let Some(node) = head {
82+
let mut nums = self.to_array(node.borrow().next.as_ref());
83+
nums.insert(0, node.borrow().val);
84+
return nums;
85+
}
86+
return Vec::new();
87+
}
88+
}
89+
90+
/* Driver Code */
91+
fn main() {
92+
/* 初始化队列 */
93+
let mut queue = LinkedListQueue::new();
94+
95+
/* 元素入队 */
96+
queue.push(1);
97+
queue.push(3);
98+
queue.push(2);
99+
queue.push(5);
100+
queue.push(4);
101+
print!("队列 queue = ");
102+
print_util::print_array(&queue.to_array(queue.peek()));
103+
104+
/* 访问队首元素 */
105+
let peek = queue.peek().unwrap().borrow().val;
106+
print!("\n队首元素 peek = {}", peek);
107+
108+
/* 元素出队 */
109+
let poll = queue.poll().unwrap();
110+
print!("\n出队元素 poll = {},出队后 queue = ", poll);
111+
print_util::print_array(&queue.to_array(queue.peek()));
112+
113+
/* 获取队列的长度 */
114+
let size = queue.size();
115+
print!("\n队列长度 size = {}", size);
116+
117+
/* 判断队列是否为空 */
118+
let is_empty = queue.is_empty();
119+
print!("\n队列是否为空 = {}", is_empty);
120+
}

zig/chapter_stack_and_queue/linkedlist_queue.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn LinkedListQueue(comptime T: type) type {
5050
}
5151

5252
// 入队
53-
pub fn offer(self: *Self, num: T) !void {
53+
pub fn push(self: *Self, num: T) !void {
5454
// 尾结点后添加 num
5555
var node = try self.mem_allocator.create(inc.ListNode(T));
5656
node.init(num);
@@ -102,11 +102,11 @@ pub fn main() !void {
102102
defer queue.deinit();
103103

104104
// 元素入队
105-
try queue.offer(1);
106-
try queue.offer(3);
107-
try queue.offer(2);
108-
try queue.offer(5);
109-
try queue.offer(4);
105+
try queue.push(1);
106+
try queue.push(3);
107+
try queue.push(2);
108+
try queue.push(5);
109+
try queue.push(4);
110110
std.debug.print("队列 queue = ", .{});
111111
inc.PrintUtil.printArray(i32, try queue.toArray());
112112

zig/chapter_stack_and_queue/linkedlist_stack.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const std = @import("std");
66
const inc = @import("include");
77

88
// 基于链表实现的栈
9-
// 编译期泛型
109
pub fn LinkedListStack(comptime T: type) type {
1110
return struct {
1211
const Self = @This();

0 commit comments

Comments
 (0)