🚀feat: add rust codes for array_deque (#418)

* update zig codes style

* feat: add rust codes for array_deque

* Update array_deque.rs

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
sjinzh 2023-03-14 20:45:27 +08:00 committed by GitHub
parent 567497a6b8
commit c2be6ebfbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 235 additions and 73 deletions

View File

@ -69,6 +69,11 @@ path = "chapter_stack_and_queue/linkedlist_queue.rs"
name = "deque"
path = "chapter_stack_and_queue/deque.rs"
# Run Command: cargo run --bin array_deque
[[bin]]
name = "array_deque"
path = "chapter_stack_and_queue/array_deque.rs"
# Run Command: cargo run --bin linkedlist_deque
[[bin]]
name = "linkedlist_deque"

View File

@ -0,0 +1,157 @@
/*
* File: array_deque.rs
* Created Time: 2023-03-11
* Author: sjinzh (sjinzh@gmail.com)
*/
include!("../include/include.rs");
/* 基于环形数组实现的双向队列 */
struct ArrayDeque {
nums: Vec<i32>, // 用于存储双向队列元素的数组
front: usize, // 队首指针,指向队首元素
que_size: usize, // 双向队列长度
}
impl ArrayDeque {
/* 构造方法 */
pub fn new(capacity: usize) -> Self {
Self {
nums: vec![0; capacity],
front: 0,
que_size: 0,
}
}
/* 获取双向队列的容量 */
pub fn capacity(&self) -> usize {
self.nums.len()
}
/* 获取双向队列的长度 */
pub fn size(&self) -> usize {
self.que_size
}
/* 判断双向队列是否为空 */
pub fn is_empty(&self) -> bool {
self.que_size == 0
}
/* 计算环形数组索引 */
fn index(&self, i: i32) -> usize {
// 通过取余操作实现数组首尾相连
// 当 i 越过数组尾部后,回到头部
// 当 i 越过数组头部后,回到尾部
return ((i + self.capacity() as i32) % self.capacity() as i32) as usize;
}
/* 队首入队 */
pub fn push_first(&mut self, num: i32) {
if self.que_size == self.capacity() {
println!("双向队列已满");
return
}
// 队首指针向左移动一位
// 通过取余操作,实现 front 越过数组头部后回到尾部
self.front = self.index(self.front as i32 - 1);
// 将 num 添加至队首
self.nums[self.front] = num;
self.que_size += 1;
}
/* 队尾入队 */
pub fn push_last(&mut self, num: i32) {
if self.que_size == self.capacity() {
println!("双向队列已满");
return
}
// 计算尾指针,指向队尾索引 + 1
let rear = self.index(self.front as i32 + self.que_size as i32);
// 将 num 添加至队尾
self.nums[rear] = num;
self.que_size += 1;
}
/* 队首出队 */
fn pop_first(&mut self) -> i32 {
let num = self.peek_first();
// 队首指针向后移动一位
self.front = self.index(self.front as i32 + 1);
self.que_size -= 1;
num
}
/* 队尾出队 */
fn pop_last(&mut self) -> i32 {
let num = self.peek_last();
self.que_size -= 1;
num
}
/* 访问队首元素 */
fn peek_first(&self) -> i32 {
if self.is_empty() { panic!("双向队列为空") };
self.nums[self.front]
}
/* 访问队尾元素 */
fn peek_last(&self) -> i32 {
if self.is_empty() { panic!("双向队列为空") };
// 计算尾元素索引
let last = self.index(self.front as i32 + self.que_size as i32 - 1);
self.nums[last]
}
/* 返回数组用于打印 */
fn to_array(&self) -> Vec<i32> {
// 仅转换有效长度范围内的列表元素
let mut res = vec![0; self.que_size];
let mut j = self.front;
for i in 0..self.que_size {
res[i] = self.nums[self.index(j as i32)];
j += 1;
}
res
}
}
fn main() {
/* 初始化双向队列 */
let mut deque = ArrayDeque::new(10);
deque.push_last(3);
deque.push_last(2);
deque.push_last(5);
print!("双向队列 deque = ");
print_util::print_array(&deque.to_array());
/* 访问元素 */
let peek_first = deque.peek_first();
print!("\n队首元素 peek_first = {}", peek_first);
let peek_last = deque.peek_last();
print!("\n队尾元素 peek_last = {}", peek_last);
/* 元素入队 */
deque.push_last(4);
print!("\n元素 4 队尾入队后 deque = ");
print_util::print_array(&deque.to_array());
deque.push_first(1);
print!("\n元素 1 队首入队后 deque = ");
print_util::print_array(&deque.to_array());
/* 元素出队 */
let pop_last = deque.pop_last();
print!("\n队尾出队元素 = {},队尾出队后 deque = ", pop_last);
print_util::print_array(&deque.to_array());
let pop_first = deque.pop_first();
print!("\n队首出队元素 = {},队首出队后 deque = ", pop_first);
print_util::print_array(&deque.to_array());
/* 获取双向队列的长度 */
let size = deque.size();
print!("\n双向队列长度 size = {}", size);
/* 判断双向队列是否为空 */
let is_empty = deque.is_empty();
print!("\n双向队列是否为空 = {}", is_empty);
}

View File

@ -11,9 +11,9 @@ pub fn MyList(comptime T: type) type {
const Self = @This();
nums: []T = undefined, //
numsCapacity: usize = 10, //
numSize: usize = 0, //
extendRatio: usize = 2, //
nums_capacity: usize = 10, //
num_size: usize = 0, //
extend_ratio: usize = 2, //
mem_arena: ?std.heap.ArenaAllocator = null,
mem_allocator: std.mem.Allocator = undefined, //
@ -23,7 +23,7 @@ pub fn MyList(comptime T: type) type {
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
self.mem_allocator = self.mem_arena.?.allocator();
}
self.nums = try self.mem_allocator.alloc(T, self.numsCapacity);
self.nums = try self.mem_allocator.alloc(T, self.nums_capacity);
std.mem.set(T, self.nums, @as(T, 0));
}
@ -35,12 +35,12 @@ pub fn MyList(comptime T: type) type {
//
pub fn size(self: *Self) usize {
return self.numSize;
return self.num_size;
}
//
pub fn capacity(self: *Self) usize {
return self.numsCapacity;
return self.nums_capacity;
}
// 访
@ -63,7 +63,7 @@ pub fn MyList(comptime T: type) type {
if (self.size() == self.capacity()) try self.extendCapacity();
self.nums[self.size()] = num;
//
self.numSize += 1;
self.num_size += 1;
}
//
@ -78,7 +78,7 @@ pub fn MyList(comptime T: type) type {
}
self.nums[index] = num;
//
self.numSize += 1;
self.num_size += 1;
}
//
@ -91,22 +91,22 @@ pub fn MyList(comptime T: type) type {
self.nums[j] = self.nums[j + 1];
}
//
self.numSize -= 1;
self.num_size -= 1;
//
return num;
}
//
pub fn extendCapacity(self: *Self) !void {
// size * extendRatio
var newCapacity = self.capacity() * self.extendRatio;
// size * extend_ratio
var newCapacity = self.capacity() * self.extend_ratio;
var extend = try self.mem_allocator.alloc(T, newCapacity);
std.mem.set(T, extend, @as(T, 0));
//
std.mem.copy(T, extend, self.nums);
self.nums = extend;
//
self.numsCapacity = newCapacity;
self.nums_capacity = newCapacity;
}
//

View File

@ -36,45 +36,45 @@ pub fn main() !void {
//
//
const PQlt = std.PriorityQueue(i32, void, lessThan);
var minHeap = PQlt.init(std.heap.page_allocator, {});
defer minHeap.deinit();
var min_heap = PQlt.init(std.heap.page_allocator, {});
defer min_heap.deinit();
//
const PQgt = std.PriorityQueue(i32, void, greaterThan);
var maxHeap = PQgt.init(std.heap.page_allocator, {});
defer maxHeap.deinit();
var max_heap = PQgt.init(std.heap.page_allocator, {});
defer max_heap.deinit();
std.debug.print("\n以下测试样例为大顶堆", .{});
//
try testPush(i32, mem_allocator, &maxHeap, 1);
try testPush(i32, mem_allocator, &maxHeap, 3);
try testPush(i32, mem_allocator, &maxHeap, 2);
try testPush(i32, mem_allocator, &maxHeap, 5);
try testPush(i32, mem_allocator, &maxHeap, 4);
try testPush(i32, mem_allocator, &max_heap, 1);
try testPush(i32, mem_allocator, &max_heap, 3);
try testPush(i32, mem_allocator, &max_heap, 2);
try testPush(i32, mem_allocator, &max_heap, 5);
try testPush(i32, mem_allocator, &max_heap, 4);
//
var peek = maxHeap.peek().?;
var peek = max_heap.peek().?;
std.debug.print("\n堆顶元素为 {}\n", .{peek});
//
try testPop(i32, mem_allocator, &maxHeap);
try testPop(i32, mem_allocator, &maxHeap);
try testPop(i32, mem_allocator, &maxHeap);
try testPop(i32, mem_allocator, &maxHeap);
try testPop(i32, mem_allocator, &maxHeap);
try testPop(i32, mem_allocator, &max_heap);
try testPop(i32, mem_allocator, &max_heap);
try testPop(i32, mem_allocator, &max_heap);
try testPop(i32, mem_allocator, &max_heap);
try testPop(i32, mem_allocator, &max_heap);
//
var size = maxHeap.len;
var size = max_heap.len;
std.debug.print("\n堆元素数量为 {}\n", .{size});
//
var is_empty = if (maxHeap.len == 0) true else false;
var is_empty = if (max_heap.len == 0) true else false;
std.debug.print("\n堆是否为空 {}\n", .{is_empty});
//
try minHeap.addSlice(&[_]i32{ 1, 3, 2, 5, 4 });
try min_heap.addSlice(&[_]i32{ 1, 3, 2, 5, 4 });
std.debug.print("\n输入列表并建立小顶堆后\n", .{});
try inc.PrintUtil.printHeap(i32, mem_allocator, minHeap);
try inc.PrintUtil.printHeap(i32, mem_allocator, min_heap);
_ = try std.io.getStdIn().reader().readByte();
}

View File

@ -10,14 +10,14 @@ pub fn MaxHeap(comptime T: type) type {
return struct {
const Self = @This();
maxHeap: ?std.ArrayList(T) = null, // 使
max_heap: ?std.ArrayList(T) = null, // 使
//
pub fn init(self: *Self, allocator: std.mem.Allocator, nums: []const T) !void {
if (self.maxHeap != null) return;
self.maxHeap = std.ArrayList(T).init(allocator);
if (self.max_heap != null) return;
self.max_heap = std.ArrayList(T).init(allocator);
//
try self.maxHeap.?.appendSlice(nums);
try self.max_heap.?.appendSlice(nums);
//
var i: usize = parent(self.size() - 1) + 1;
while (i > 0) : (i -= 1) {
@ -27,7 +27,7 @@ pub fn MaxHeap(comptime T: type) type {
//
pub fn deinit(self: *Self) void {
if (self.maxHeap != null) self.maxHeap.?.deinit();
if (self.max_heap != null) self.max_heap.?.deinit();
}
//
@ -48,16 +48,16 @@ pub fn MaxHeap(comptime T: type) type {
//
fn swap(self: *Self, i: usize, j: usize) !void {
var a = self.maxHeap.?.items[i];
var b = self.maxHeap.?.items[j];
var a = self.max_heap.?.items[i];
var b = self.max_heap.?.items[j];
var tmp = a;
try self.maxHeap.?.replaceRange(i, 1, &[_]T{b});
try self.maxHeap.?.replaceRange(j, 1, &[_]T{tmp});
try self.max_heap.?.replaceRange(i, 1, &[_]T{b});
try self.max_heap.?.replaceRange(j, 1, &[_]T{tmp});
}
//
pub fn size(self: *Self) usize {
return self.maxHeap.?.items.len;
return self.max_heap.?.items.len;
}
//
@ -67,13 +67,13 @@ pub fn MaxHeap(comptime T: type) type {
// 访
pub fn peek(self: *Self) T {
return self.maxHeap.?.items[0];
return self.max_heap.?.items[0];
}
//
pub fn push(self: *Self, val: T) !void {
//
try self.maxHeap.?.append(val);
try self.max_heap.?.append(val);
//
try self.siftUp(self.size() - 1);
}
@ -85,7 +85,7 @@ pub fn MaxHeap(comptime T: type) type {
// i
var p = parent(i);
//
if (p < 0 or self.maxHeap.?.items[i] <= self.maxHeap.?.items[p]) break;
if (p < 0 or self.max_heap.?.items[i] <= self.max_heap.?.items[p]) break;
//
try self.swap(i, p);
//
@ -100,7 +100,7 @@ pub fn MaxHeap(comptime T: type) type {
//
try self.swap(0, self.size() - 1);
//
var val = self.maxHeap.?.pop();
var val = self.max_heap.?.pop();
//
try self.siftDown(0);
//
@ -115,8 +115,8 @@ pub fn MaxHeap(comptime T: type) type {
var l = left(i);
var r = right(i);
var ma = i;
if (l < self.size() and self.maxHeap.?.items[l] > self.maxHeap.?.items[ma]) ma = l;
if (r < self.size() and self.maxHeap.?.items[r] > self.maxHeap.?.items[ma]) ma = r;
if (l < self.size() and self.max_heap.?.items[l] > self.max_heap.?.items[ma]) ma = l;
if (r < self.size() and self.max_heap.?.items[r] > self.max_heap.?.items[ma]) ma = r;
// i l, r
if (ma == i) break;
//
@ -140,7 +140,7 @@ pub fn MaxHeap(comptime T: type) type {
const PQgt = std.PriorityQueue(T, void, greaterThan);
var queue = PQgt.init(std.heap.page_allocator, {});
defer queue.deinit();
try queue.addSlice(self.maxHeap.?.items);
try queue.addSlice(self.max_heap.?.items);
try inc.PrintUtil.printHeap(T, mem_allocator, queue);
}
};
@ -154,33 +154,33 @@ pub fn main() !void {
const mem_allocator = mem_arena.allocator();
//
var maxHeap = MaxHeap(i32){};
try maxHeap.init(std.heap.page_allocator, &[_]i32{ 9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2 });
defer maxHeap.deinit();
var max_heap = MaxHeap(i32){};
try max_heap.init(std.heap.page_allocator, &[_]i32{ 9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2 });
defer max_heap.deinit();
std.debug.print("\n输入列表并建堆后\n", .{});
try maxHeap.print(mem_allocator);
try max_heap.print(mem_allocator);
//
var peek = maxHeap.peek();
var peek = max_heap.peek();
std.debug.print("\n堆顶元素为 {}\n", .{peek});
//
const val = 7;
try maxHeap.push(val);
try max_heap.push(val);
std.debug.print("\n元素 {} 入堆后\n", .{val});
try maxHeap.print(mem_allocator);
try max_heap.print(mem_allocator);
//
peek = try maxHeap.pop();
peek = try max_heap.pop();
std.debug.print("\n堆顶元素 {} 出堆后\n", .{peek});
try maxHeap.print(mem_allocator);
try max_heap.print(mem_allocator);
//
var size = maxHeap.size();
var size = max_heap.size();
std.debug.print("\n堆元素数量为 {}", .{size});
//
var is_empty = maxHeap.isEmpty();
var is_empty = max_heap.isEmpty();
std.debug.print("\n堆是否为空 {}\n", .{is_empty});
_ = try std.io.getStdIn().reader().readByte();

View File

@ -13,7 +13,7 @@ pub fn ArrayQueue(comptime T: type) type {
nums: []T = undefined, //
cap: usize = 0, //
front: usize = 0, //
queSize: usize = 0, // + 1
que_size: usize = 0, // + 1
mem_arena: ?std.heap.ArenaAllocator = null,
mem_allocator: std.mem.Allocator = undefined, //
@ -41,12 +41,12 @@ pub fn ArrayQueue(comptime T: type) type {
//
pub fn size(self: *Self) usize {
return self.queSize;
return self.que_size;
}
//
pub fn isEmpty(self: *Self) bool {
return self.queSize == 0;
return self.que_size == 0;
}
//
@ -57,10 +57,10 @@ pub fn ArrayQueue(comptime T: type) type {
}
// + 1
// rear
var rear = (self.front + self.queSize) % self.capacity();
var rear = (self.front + self.que_size) % self.capacity();
// num
self.nums[rear] = num;
self.queSize += 1;
self.que_size += 1;
}
//
@ -68,7 +68,7 @@ pub fn ArrayQueue(comptime T: type) type {
var num = self.peek();
//
self.front = (self.front + 1) % self.capacity();
self.queSize -= 1;
self.que_size -= 1;
return num;
}

View File

@ -26,17 +26,17 @@ pub fn main() !void {
inc.PrintUtil.printQueue(i32, deque);
// 访
var peekFirst = deque.first.?.data; //
std.debug.print("\n队首元素 peekFirst = {}", .{peekFirst});
var peekLast = deque.last.?.data; //
std.debug.print("\n队尾元素 peekLast = {}", .{peekLast});
var peek_first = deque.first.?.data; //
std.debug.print("\n队首元素 peek_first = {}", .{peek_first});
var peek_last = deque.last.?.data; //
std.debug.print("\n队尾元素 peek_last = {}", .{peek_last});
//
var popFirst = deque.popFirst().?.data; //
std.debug.print("\n队首出队元素 popFirst = {},队首出队后 deque = ", .{popFirst});
var pop_first = deque.popFirst().?.data; //
std.debug.print("\n队首出队元素 pop_first = {},队首出队后 deque = ", .{pop_first});
inc.PrintUtil.printQueue(i32, deque);
var popLast = deque.pop().?.data; //
std.debug.print("\n队尾出队元素 popLast = {},队尾出队后 deque = ", .{popLast});
var pop_last = deque.pop().?.data; //
std.debug.print("\n队尾出队元素 pop_last = {},队尾出队后 deque = ", .{pop_last});
inc.PrintUtil.printQueue(i32, deque);
//