Skip to content

Commit ff917b8

Browse files
committed
add rust codes for chapter queue
1 parent 355e6a8 commit ff917b8

File tree

6 files changed

+60
-45
lines changed

6 files changed

+60
-45
lines changed

c/chapter_array_and_linkedlist/my_list.c

Lines changed: 0 additions & 39 deletions
This file was deleted.

rust/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ path = "chapter_array_and_linkedlist/list.rs"
3737
name = "stack"
3838
path = "chapter_stack_and_queue/stack.rs"
3939

40+
# Run Command: cargo run --bin queue
41+
[[bin]]
42+
name = "queue"
43+
path = "chapter_stack_and_queue/queue.rs"
44+
4045
# Run Command: cargo run --bin hash_map
4146
[[bin]]
4247
name = "hash_map"

rust/chapter_stack_and_queue/queue.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* File: queue.rs
3+
* Created Time: 2023-02-05
4+
* Author: sjinzh (sjinzh@gmail.com)
5+
*/
6+
7+
use std::collections::LinkedList;
8+
9+
/* Driver Code */
10+
pub fn main() {
11+
// 初始化队列
12+
let mut queue: LinkedList<i32> = LinkedList::new();
13+
14+
// 元素入队
15+
queue.push_back(1);
16+
queue.push_back(3);
17+
queue.push_back(2);
18+
queue.push_back(5);
19+
queue.push_back(4);
20+
print!("队列 queue = ");
21+
inc::print_util::print_queue(&queue);
22+
23+
// 访问队首元素
24+
let peek = queue.front().unwrap();
25+
println!("\n队首元素 peek = {peek}");
26+
27+
// 元素出队
28+
let poll = queue.pop_front().unwrap();
29+
print!("出队元素 poll = {poll},出队后 queue = ");
30+
inc::print_util::print_queue(&queue);
31+
32+
// 获取队列的长度
33+
let size = queue.len();
34+
print!("\n队列长度 size = {size}");
35+
36+
/* 判断队列是否为空 */
37+
let is_empty = queue.is_empty();
38+
print!("\n队列是否为空 = {is_empty}");
39+
}

rust/chapter_stack_and_queue/stack.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ pub fn main() {
3333
print!("\n栈的长度 size = {size}");
3434

3535
// 判断栈是否为空
36-
let empty = stack.is_empty();
37-
print!("\n栈是否为空 = {empty}");
36+
let is_empty = stack.is_empty();
37+
print!("\n栈是否为空 = {is_empty}");
3838
}

rust/include/print_util.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use std::fmt::Display;
88
use std::collections::HashMap;
9+
use std::collections::LinkedList;
910

1011
/* Print an array */
1112
pub fn print_array<T: Display>(nums: &[T]) {
@@ -24,4 +25,13 @@ pub fn print_hash_map<TKey: Display, TValue: Display>(map: &HashMap<TKey, TValue
2425
for (key, value) in map {
2526
println!("{key} -> {value}");
2627
}
28+
}
29+
30+
/* Print a queue or deque */
31+
pub fn print_queue<T: Display>(queue: &LinkedList<T>) {
32+
print!("[");
33+
let iter = queue.iter();
34+
for (i, data) in iter.enumerate() {
35+
print!("{}{}", data, if i == queue.len() - 1 {"]"} else {", "} );
36+
}
2737
}

zig/chapter_stack_and_queue/queue.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ pub fn main() !void {
3030
inc.PrintUtil.printQueue(i32, queue);
3131

3232
// 访问队首元素
33-
var front = queue.first.?.data;
34-
std.debug.print("\n队首元素 front = {}", .{front});
33+
var peek = queue.first.?.data;
34+
std.debug.print("\n队首元素 peek = {}", .{peek});
3535

3636
// 元素出队
37-
front = queue.popFirst().?.data;
38-
std.debug.print("\n出队元素 front = {},出队后 queue = ", .{front});
37+
var poll = queue.popFirst().?.data;
38+
std.debug.print("\n出队元素 poll = {},出队后 queue = ", .{poll});
3939
inc.PrintUtil.printQueue(i32, queue);
4040

4141
// 获取队列的长度

0 commit comments

Comments
 (0)