From 6924d15f636476e28a41880b7a0df98add3a7d0d Mon Sep 17 00:00:00 2001 From: sjinzh <99076655+sjinzh@users.noreply.github.com> Date: Mon, 13 Mar 2023 20:15:11 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80feat:=20add=20rust=20codes=20for=20?= =?UTF-8?q?linkedlist=5Fstack,=20linkedlist=5Fqueue=20and=20linkedlist=5Fd?= =?UTF-8?q?eque=20(#410)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add rust codes for space_complexity * feat: add rust codes for linkedlist_stack * update * feat: add rust codes for linkedlist_queue * feat: add rust codes for linkedlist_deque * update --- codes/rust/Cargo.toml | 15 ++ .../linked_list.rs | 2 + .../space_complexity.rs | 2 + .../linkedlist_deque.rs | 207 ++++++++++++++++++ .../linkedlist_queue.rs | 121 ++++++++++ .../linkedlist_stack.rs | 108 +++++++++ codes/rust/include/include.rs | 4 +- .../linkedlist_deque.zig | 94 +++----- .../linkedlist_queue.zig | 10 +- .../linkedlist_stack.zig | 36 +-- 10 files changed, 515 insertions(+), 84 deletions(-) create mode 100644 codes/rust/chapter_stack_and_queue/linkedlist_deque.rs create mode 100644 codes/rust/chapter_stack_and_queue/linkedlist_queue.rs create mode 100644 codes/rust/chapter_stack_and_queue/linkedlist_stack.rs diff --git a/codes/rust/Cargo.toml b/codes/rust/Cargo.toml index 10209b5b..36819644 100644 --- a/codes/rust/Cargo.toml +++ b/codes/rust/Cargo.toml @@ -49,16 +49,31 @@ path = "chapter_array_and_linkedlist/my_list.rs" name = "stack" path = "chapter_stack_and_queue/stack.rs" +# Run Command: cargo run --bin linkedlist_stack +[[bin]] +name = "linkedlist_stack" +path = "chapter_stack_and_queue/linkedlist_stack.rs" + # Run Command: cargo run --bin queue [[bin]] name = "queue" path = "chapter_stack_and_queue/queue.rs" +# Run Command: cargo run --bin linkedlist_queue +[[bin]] +name = "linkedlist_queue" +path = "chapter_stack_and_queue/linkedlist_queue.rs" + # Run Command: cargo run --bin deque [[bin]] name = "deque" path = "chapter_stack_and_queue/deque.rs" +# Run Command: cargo run --bin linkedlist_deque +[[bin]] +name = "linkedlist_deque" +path = "chapter_stack_and_queue/linkedlist_deque.rs" + # Run Command: cargo run --bin hash_map [[bin]] name = "hash_map" diff --git a/codes/rust/chapter_array_and_linkedlist/linked_list.rs b/codes/rust/chapter_array_and_linkedlist/linked_list.rs index 16c14e18..fe9aa5c8 100644 --- a/codes/rust/chapter_array_and_linkedlist/linked_list.rs +++ b/codes/rust/chapter_array_and_linkedlist/linked_list.rs @@ -5,8 +5,10 @@ */ include!("../include/include.rs"); + use std::rc::Rc; use std::cell::RefCell; +use list_node::ListNode; /* 在链表的结点 n0 之后插入结点 P */ #[allow(non_snake_case)] diff --git a/codes/rust/chapter_computational_complexity/space_complexity.rs b/codes/rust/chapter_computational_complexity/space_complexity.rs index 3ab7bb23..82758f20 100644 --- a/codes/rust/chapter_computational_complexity/space_complexity.rs +++ b/codes/rust/chapter_computational_complexity/space_complexity.rs @@ -9,6 +9,8 @@ include!("../include/include.rs"); use std::collections::HashMap; use std::rc::Rc; use std::cell::RefCell; +use list_node::ListNode; +use tree_node::TreeNode; /* 函数 */ fn function() ->i32 { diff --git a/codes/rust/chapter_stack_and_queue/linkedlist_deque.rs b/codes/rust/chapter_stack_and_queue/linkedlist_deque.rs new file mode 100644 index 00000000..d1f4bec6 --- /dev/null +++ b/codes/rust/chapter_stack_and_queue/linkedlist_deque.rs @@ -0,0 +1,207 @@ +/* + * File: linkedlist_deque.rs + * Created Time: 2023-03-11 + * Author: sjinzh (sjinzh@gmail.com) + */ + +include!("../include/include.rs"); + +use std::rc::Rc; +use std::cell::RefCell; + +/* 双向链表结点 */ +pub struct ListNode { + pub val: T, // 结点值 + pub next: Option>>>, // 后继结点引用(指针) + pub prev: Option>>>, // 前驱结点引用(指针) +} + +impl ListNode { + pub fn new(val: T) -> Rc>> { + Rc::new(RefCell::new(ListNode { + val, + next: None, + prev: None, + })) + } +} + +/* 基于双向链表实现的双向队列 */ +#[allow(dead_code)] +pub struct LinkedListDeque { + front: Option>>>, // 头结点 front + rear: Option>>>, // 尾结点 rear + que_size: usize, // 双向队列的长度 +} + +impl LinkedListDeque { + pub fn new() -> Self { + Self { + front: None, + rear: None, + que_size: 0, + } + } + + /* 获取双向队列的长度 */ + pub fn size(&self) -> usize { + return self.que_size; + } + + /* 判断双向队列是否为空 */ + pub fn is_empty(&self) -> bool { + return self.size() == 0; + } + + /* 入队操作 */ + pub fn push(&mut self, num: T, is_front: bool) { + let node = ListNode::new(num); + // 队首入队操作 + if is_front { + // 将 node 添加至链表头部 + match self.front.take() { + Some(old_front) => { + old_front.borrow_mut().prev = Some(node.clone()); + node.borrow_mut().next = Some(old_front); + self.front = Some(node); // 更新头结点 + } + None => { + self.rear = Some(node.clone()); + self.front = Some(node); + } + } + // 队尾入队操作 + } else { + // 将 node 添加至链表尾部 + match self.rear.take() { + Some(old_rear) => { + old_rear.borrow_mut().next = Some(node.clone()); + node.borrow_mut().prev = Some(old_rear); + self.rear = Some(node); // 更新尾结点 + } + None => { + self.front = Some(node.clone()); + self.rear = Some(node); + } + } + } + self.que_size += 1; + } + + /* 队首入队 */ + pub fn push_first(&mut self, num: T) { + self.push(num, true); + } + + /* 队尾入队 */ + pub fn push_last(&mut self, num: T) { + self.push(num, false); + } + + /* 出队操作 */ + pub fn poll(&mut self, is_front: bool) -> Option { + if self.is_empty() {return None}; + // 队首出队操作 + if is_front { + self.front.take().map(|old_front| { + match old_front.borrow_mut().next.take() { + Some(new_front) => { + new_front.borrow_mut().prev.take(); + self.front = Some(new_front); // 更新头结点 + } + None => { + self.rear.take(); + } + } + self.que_size -= 1; + Rc::try_unwrap(old_front).ok().unwrap().into_inner().val + }) + // 队尾出队操作 + } else { + self.rear.take().map(|old_rear| { + match old_rear.borrow_mut().prev.take() { + Some(new_rear) => { + new_rear.borrow_mut().next.take(); + self.rear = Some(new_rear); // 更新尾结点 + } + None => { + self.front.take(); + } + } + self.que_size -= 1; + Rc::try_unwrap(old_rear).ok().unwrap().into_inner().val + }) + } + } + + /* 队首出队 */ + pub fn poll_first(&mut self) -> Option { + return self.poll(true); + } + + /* 队尾出队 */ + pub fn poll_last(&mut self) -> Option { + return self.poll(false); + } + + /* 访问队首元素 */ + pub fn peek_first(&self) -> Option<&Rc>>> { + self.front.as_ref() + } + + /* 访问队尾元素 */ + pub fn peek_last(&self) -> Option<&Rc>>> { + self.rear.as_ref() + } + + /* 返回数组用于打印 */ + pub fn to_array(&self, head: Option<&Rc>>>) -> Vec { + if let Some(node) = head { + let mut nums = self.to_array(node.borrow().next.as_ref()); + nums.insert(0, node.borrow().val); + return nums; + } + return Vec::new(); + } +} + +/* Driver Code */ +fn main() { + /* 初始化双向队列 */ + let mut deque = LinkedListDeque::new(); + deque.push_last(3); + deque.push_last(2); + deque.push_last(5); + print!("双向队列 deque = "); + print_util::print_array(&deque.to_array(deque.peek_first())); + + /* 访问元素 */ + let peek_first = deque.peek_first().unwrap().borrow().val; + print!("\n队首元素 peek_first = {}", peek_first); + let peek_last = deque.peek_last().unwrap().borrow().val; + print!("\n队尾元素 peek_last = {}", peek_last); + + /* 元素入队 */ + deque.push_last(4); + print!("\n元素 4 队尾入队后 deque = "); + print_util::print_array(&deque.to_array(deque.peek_first())); + deque.push_first(1); + print!("\n元素 1 队首入队后 deque = "); + print_util::print_array(&deque.to_array(deque.peek_first())); + + /* 元素出队 */ + let poll_last = deque.poll_last().unwrap(); + print!("\n队尾出队元素 = {},队尾出队后 deque = ", poll_last); + print_util::print_array(&deque.to_array(deque.peek_first())); + let poll_first = deque.poll_first().unwrap(); + print!("\n队首出队元素 = {},队首出队后 deque = ", poll_first); + print_util::print_array(&deque.to_array(deque.peek_first())); + + /* 获取双向队列的长度 */ + let size = deque.size(); + print!("\n双向队列长度 size = {}", size); + + /* 判断双向队列是否为空 */ + let is_empty = deque.is_empty(); + print!("\n双向队列是否为空 = {}", is_empty); +} \ No newline at end of file diff --git a/codes/rust/chapter_stack_and_queue/linkedlist_queue.rs b/codes/rust/chapter_stack_and_queue/linkedlist_queue.rs new file mode 100644 index 00000000..6eada837 --- /dev/null +++ b/codes/rust/chapter_stack_and_queue/linkedlist_queue.rs @@ -0,0 +1,121 @@ +/* + * File: linkedlist_queue.rs + * Created Time: 2023-03-11 + * Author: sjinzh (sjinzh@gmail.com) + */ + +include!("../include/include.rs"); + +use std::rc::Rc; +use std::cell::RefCell; +use list_node::ListNode; + +/* 基于链表实现的队列 */ +#[allow(dead_code)] +pub struct LinkedListQueue { + front: Option>>>, // 头结点 front + rear: Option>>>, // 尾结点 rear + que_size: usize, // 队列的长度 +} + +impl LinkedListQueue { + pub fn new() -> Self { + Self { + front: None, + rear: None, + que_size: 0, + } + } + + /* 获取队列的长度 */ + pub fn size(&self) -> usize { + return self.que_size; + } + + /* 判断队列是否为空 */ + pub fn is_empty(&self) -> bool { + return self.size() == 0; + } + + /* 入队 */ + pub fn push(&mut self, num: T) { + // 尾结点后添加 num + let new_rear = ListNode::new(num); + match self.rear.take() { + // 如果队列不为空,则将该结点添加到尾结点后 + Some(old_rear) => { + old_rear.borrow_mut().next = Some(new_rear.clone()); + self.rear = Some(new_rear); + } + // 如果队列为空,则令头、尾结点都指向该结点 + None => { + self.front = Some(new_rear.clone()); + self.rear = Some(new_rear); + } + } + self.que_size += 1; + } + + /* 出队 */ + pub fn poll(&mut self) -> Option { + self.front.take().map(|old_front| { + match old_front.borrow_mut().next.take() { + Some(new_front) => { + self.front = Some(new_front); + } + None => { + self.rear.take(); + } + } + self.que_size -= 1; + Rc::try_unwrap(old_front).ok().unwrap().into_inner().val + }) + } + + /* 访问队首元素 */ + pub fn peek(&self) -> Option<&Rc>>> { + self.front.as_ref() + } + + /* 将链表转化为 Array 并返回 */ + pub fn to_array(&self, head: Option<&Rc>>>) -> Vec { + if let Some(node) = head { + let mut nums = self.to_array(node.borrow().next.as_ref()); + nums.insert(0, node.borrow().val); + return nums; + } + return Vec::new(); + } +} + +/* Driver Code */ +fn main() { + /* 初始化队列 */ + let mut queue = LinkedListQueue::new(); + + /* 元素入队 */ + queue.push(1); + queue.push(3); + queue.push(2); + queue.push(5); + queue.push(4); + print!("队列 queue = "); + print_util::print_array(&queue.to_array(queue.peek())); + + /* 访问队首元素 */ + let peek = queue.peek().unwrap().borrow().val; + print!("\n队首元素 peek = {}", peek); + + /* 元素出队 */ + let poll = queue.poll().unwrap(); + print!("\n出队元素 poll = {},出队后 queue = ", poll); + print_util::print_array(&queue.to_array(queue.peek())); + + /* 获取队列的长度 */ + let size = queue.size(); + print!("\n队列长度 size = {}", size); + + /* 判断队列是否为空 */ + let is_empty = queue.is_empty(); + print!("\n队列是否为空 = {}", is_empty); +} \ No newline at end of file diff --git a/codes/rust/chapter_stack_and_queue/linkedlist_stack.rs b/codes/rust/chapter_stack_and_queue/linkedlist_stack.rs new file mode 100644 index 00000000..ba52a815 --- /dev/null +++ b/codes/rust/chapter_stack_and_queue/linkedlist_stack.rs @@ -0,0 +1,108 @@ +/* + * File: linkedlist_stack.rs + * Created Time: 2023-03-11 + * Author: sjinzh (sjinzh@gmail.com) + */ + +include!("../include/include.rs"); + +use std::rc::Rc; +use std::cell::RefCell; +use list_node::ListNode; + +/* 基于链表实现的栈 */ +#[allow(dead_code)] +pub struct LinkedListStack { + stack_peek: Option>>>, // 将头结点作为栈顶 + stk_size: usize, // 栈的长度 +} + +impl LinkedListStack { + pub fn new() -> Self { + Self { + stack_peek: None, + stk_size: 0, + } + } + + /* 获取栈的长度 */ + pub fn size(&self) -> usize { + return self.stk_size; + } + + /* 判断栈是否为空 */ + pub fn is_empty(&self) -> bool { + return self.size() == 0; + } + + /* 入栈 */ + pub fn push(&mut self, num: T) { + let node = ListNode::new(num); + node.borrow_mut().next = self.stack_peek.take(); + self.stack_peek = Some(node); + self.stk_size += 1; + } + + /* 出栈 */ + pub fn pop(&mut self) -> Option { + self.stack_peek.take().map(|old_head| { + match old_head.borrow_mut().next.take() { + Some(new_head) => { + self.stack_peek = Some(new_head); + } + None => { + self.stack_peek = None; + } + } + self.stk_size -= 1; + Rc::try_unwrap(old_head).ok().unwrap().into_inner().val + }) + } + + /* 访问栈顶元素 */ + pub fn peek(&self) -> Option<&Rc>>> { + self.stack_peek.as_ref() + } + + /* 将 List 转化为 Array 并返回 */ + pub fn to_array(&self, head: Option<&Rc>>>) -> Vec { + if let Some(node) = head { + let mut nums = self.to_array(node.borrow().next.as_ref()); + nums.push(node.borrow().val); + return nums; + } + return Vec::new(); + } +} + +/* Driver Code */ +fn main() { + /* 初始化栈 */ + let mut stack = LinkedListStack::new(); + + /* 元素入栈 */ + stack.push(1); + stack.push(3); + stack.push(2); + stack.push(5); + stack.push(4); + print!("栈 stack = "); + print_util::print_array(&stack.to_array(stack.peek())); + + /* 访问栈顶元素 */ + let peek = stack.peek().unwrap().borrow().val; + print!("\n栈顶元素 peek = {}", peek); + + /* 元素出栈 */ + let pop = stack.pop().unwrap(); + print!("\n出栈元素 pop = {},出栈后 stack = ", pop); + print_util::print_array(&stack.to_array(stack.peek())); + + /* 获取栈的长度 */ + let size = stack.size(); + print!("\n栈的长度 size = {}", size); + + /* 判断是否为空 */ + let is_empty = stack.is_empty(); + print!("\n栈是否为空 = {}", is_empty); +} \ No newline at end of file diff --git a/codes/rust/include/include.rs b/codes/rust/include/include.rs index e1ffd8cb..f227e3d4 100644 --- a/codes/rust/include/include.rs +++ b/codes/rust/include/include.rs @@ -6,6 +6,4 @@ pub mod print_util; pub mod list_node; -pub use list_node::ListNode; -pub mod tree_node; -pub use tree_node::TreeNode; \ No newline at end of file +pub mod tree_node; \ No newline at end of file diff --git a/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig b/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig index 18d08004..169a1d9e 100644 --- a/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig +++ b/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig @@ -30,7 +30,7 @@ pub fn LinkedListDeque(comptime T: type) type { front: ?*ListNode(T) = null, // 头结点 front rear: ?*ListNode(T) = null, // 尾结点 rear - deqSize: usize = 0, // 双向队列的长度 + que_size: usize = 0, // 双向队列的长度 mem_arena: ?std.heap.ArenaAllocator = null, mem_allocator: std.mem.Allocator = undefined, // 内存分配器 @@ -42,7 +42,7 @@ pub fn LinkedListDeque(comptime T: type) type { } self.front = null; self.rear = null; - self.deqSize = 0; + self.que_size = 0; } // 析构方法(释放内存) @@ -53,7 +53,7 @@ pub fn LinkedListDeque(comptime T: type) type { // 获取双向队列的长度 pub fn size(self: *Self) usize { - return self.deqSize; + return self.que_size; } // 判断双向队列是否为空 @@ -62,7 +62,7 @@ pub fn LinkedListDeque(comptime T: type) type { } // 入队操作 - pub fn push(self: *Self, num: T, isFront: bool) !void { + pub fn push(self: *Self, num: T, is_front: bool) !void { var node = try self.mem_allocator.create(ListNode(T)); node.init(num); // 若链表为空,则令 front, rear 都指向 node @@ -70,7 +70,7 @@ pub fn LinkedListDeque(comptime T: type) type { self.front = node; self.rear = node; // 队首入队操作 - } else if (isFront) { + } else if (is_front) { // 将 node 添加至链表头部 self.front.?.prev = node; node.next = self.front; @@ -82,7 +82,7 @@ pub fn LinkedListDeque(comptime T: type) type { node.prev = self.rear; self.rear = node; // 更新尾结点 } - self.deqSize += 1; // 更新队列长度 + self.que_size += 1; // 更新队列长度 } // 队首入队 @@ -96,11 +96,11 @@ pub fn LinkedListDeque(comptime T: type) type { } // 出队操作 - pub fn poll(self: *Self, isFront: bool) T { + pub fn poll(self: *Self, is_front: bool) T { if (self.isEmpty()) @panic("双向队列为空"); var val: T = undefined; // 队首出队操作 - if (isFront) { + if (is_front) { val = self.front.?.val; // 暂存头结点值 // 删除头结点 var fNext = self.front.?.next; @@ -120,7 +120,7 @@ pub fn LinkedListDeque(comptime T: type) type { } self.rear = rPrev; // 更新尾结点 } - self.deqSize -= 1; // 更新队列长度 + self.que_size -= 1; // 更新队列长度 return val; } @@ -146,7 +146,7 @@ pub fn LinkedListDeque(comptime T: type) type { return self.rear.?.val; } - // 将链表转换为数组 + // 返回数组用于打印 pub fn toArray(self: *Self) ![]T { var node = self.front; var res = try self.mem_allocator.alloc(T, self.size()); @@ -158,19 +158,6 @@ pub fn LinkedListDeque(comptime T: type) type { } return res; } - - // 打印双向队列 - pub fn print(self: *Self) !void { - var nums = try self.toArray(); - std.debug.print("[", .{}); - if (nums.len > 0) { - for (nums) |num, j| { - std.debug.print("{}{s}", .{num, if (j == nums.len - 1) "]" else " <-> " }); - } - } else { - std.debug.print("]", .{}); - } - } }; } @@ -180,46 +167,37 @@ pub fn main() !void { var deque = LinkedListDeque(i32){}; try deque.init(std.heap.page_allocator); defer deque.deinit(); - std.debug.print("初始化空队列\n", .{}); - try deque.print(); + try deque.pushLast(3); + try deque.pushLast(2); + try deque.pushLast(5); + std.debug.print("双向队列 deque = ", .{}); + inc.PrintUtil.printArray(i32, try deque.toArray()); - var nums = [_]i32{ 1, 2, 3}; // 测试数据 + // 访问元素 + var peek_first = deque.peekFirst(); + std.debug.print("\n队首元素 peek_first = {}", .{peek_first}); + var peek_last = deque.peekLast(); + std.debug.print("\n队尾元素 peek_last = {}", .{peek_last}); - // 队尾入队 - for (nums) |num| { - try deque.pushLast(num); - std.debug.print("\n元素 {} 队尾入队后,队列为\n", .{num}); - try deque.print(); - } - // 获取队尾元素 - var last = deque.peekLast(); - std.debug.print("\n队尾元素为 {}", .{last}); - // 队尾出队 - while (!deque.isEmpty()) { - last = deque.pollLast(); - std.debug.print("\n队尾出队元素为 {} ,队列为\n", .{last}); - try deque.print(); - } + // 元素入队 + try deque.pushLast(4); + std.debug.print("\n元素 4 队尾入队后 deque = ", .{}); + inc.PrintUtil.printArray(i32, try deque.toArray()); + try deque.pushFirst(1); + std.debug.print("\n元素 1 队首入队后 deque = ", .{}); + inc.PrintUtil.printArray(i32, try deque.toArray()); - // 队首入队 - for (nums) |num| { - try deque.pushFirst(num); - std.debug.print("\n元素 {} 队首入队后,队列为\n", .{num}); - try deque.print(); - } - // 获取队首元素 - var first = deque.peekFirst(); - std.debug.print("\n队首元素为 {}", .{first}); - // 队首出队 - while (!deque.isEmpty()) { - first = deque.pollFirst(); - std.debug.print("\n队首出队元素为 {} ,队列为\n", .{first}); - try deque.print(); - } + // 元素出队 + var poll_last = deque.pollLast(); + std.debug.print("\n队尾出队元素 = {},队尾出队后 deque = ", .{poll_last}); + inc.PrintUtil.printArray(i32, try deque.toArray()); + var poll_first = deque.pollFirst(); + std.debug.print("\n队首出队元素 = {},队首出队后 deque = ", .{poll_first}); + inc.PrintUtil.printArray(i32, try deque.toArray()); - // 获取队列的长度 + // 获取双向队列的长度 var size = deque.size(); - std.debug.print("\n队列长度 size = {}", .{size}); + std.debug.print("\n双向队列长度 size = {}", .{size}); // 判断双向队列是否为空 var is_empty = deque.isEmpty(); diff --git a/codes/zig/chapter_stack_and_queue/linkedlist_queue.zig b/codes/zig/chapter_stack_and_queue/linkedlist_queue.zig index db2622d0..e9639d70 100644 --- a/codes/zig/chapter_stack_and_queue/linkedlist_queue.zig +++ b/codes/zig/chapter_stack_and_queue/linkedlist_queue.zig @@ -12,7 +12,7 @@ pub fn LinkedListQueue(comptime T: type) type { front: ?*inc.ListNode(T) = null, // 头结点 front rear: ?*inc.ListNode(T) = null, // 尾结点 rear - queSize: usize = 0, // 队列的长度 + que_size: usize = 0, // 队列的长度 mem_arena: ?std.heap.ArenaAllocator = null, mem_allocator: std.mem.Allocator = undefined, // 内存分配器 @@ -24,7 +24,7 @@ pub fn LinkedListQueue(comptime T: type) type { } self.front = null; self.rear = null; - self.queSize = 0; + self.que_size = 0; } // 析构方法(释放内存) @@ -35,7 +35,7 @@ pub fn LinkedListQueue(comptime T: type) type { // 获取队列的长度 pub fn size(self: *Self) usize { - return self.queSize; + return self.que_size; } // 判断队列是否为空 @@ -63,7 +63,7 @@ pub fn LinkedListQueue(comptime T: type) type { self.rear.?.next = node; self.rear = node; } - self.queSize += 1; + self.que_size += 1; } // 出队 @@ -71,7 +71,7 @@ pub fn LinkedListQueue(comptime T: type) type { var num = self.peek(); // 删除头结点 self.front = self.front.?.next; - self.queSize -= 1; + self.que_size -= 1; return num; } diff --git a/codes/zig/chapter_stack_and_queue/linkedlist_stack.zig b/codes/zig/chapter_stack_and_queue/linkedlist_stack.zig index 55ce994a..67ab9882 100644 --- a/codes/zig/chapter_stack_and_queue/linkedlist_stack.zig +++ b/codes/zig/chapter_stack_and_queue/linkedlist_stack.zig @@ -10,8 +10,8 @@ pub fn LinkedListStack(comptime T: type) type { return struct { const Self = @This(); - stackTop: ?*inc.ListNode(T) = null, // 将头结点作为栈顶 - stkSize: usize = 0, // 栈的长度 + stack_top: ?*inc.ListNode(T) = null, // 将头结点作为栈顶 + stk_size: usize = 0, // 栈的长度 mem_arena: ?std.heap.ArenaAllocator = null, mem_allocator: std.mem.Allocator = undefined, // 内存分配器 @@ -21,8 +21,8 @@ pub fn LinkedListStack(comptime T: type) type { self.mem_arena = std.heap.ArenaAllocator.init(allocator); self.mem_allocator = self.mem_arena.?.allocator(); } - self.stackTop = null; - self.stkSize = 0; + self.stack_top = null; + self.stk_size = 0; } // 析构方法(释放内存) @@ -33,7 +33,7 @@ pub fn LinkedListStack(comptime T: type) type { // 获取栈的长度 pub fn size(self: *Self) usize { - return self.stkSize; + return self.stk_size; } // 判断栈是否为空 @@ -42,31 +42,31 @@ pub fn LinkedListStack(comptime T: type) type { } // 访问栈顶元素 - pub fn top(self: *Self) T { + pub fn peek(self: *Self) T { if (self.size() == 0) @panic("栈为空"); - return self.stackTop.?.val; + return self.stack_top.?.val; } // 入栈 pub fn push(self: *Self, num: T) !void { var node = try self.mem_allocator.create(inc.ListNode(T)); node.init(num); - node.next = self.stackTop; - self.stackTop = node; - self.stkSize += 1; + node.next = self.stack_top; + self.stack_top = node; + self.stk_size += 1; } // 出栈 pub fn pop(self: *Self) T { - var num = self.top(); - self.stackTop = self.stackTop.?.next; - self.stkSize -= 1; + var num = self.peek(); + self.stack_top = self.stack_top.?.next; + self.stk_size -= 1; return num; } // 将栈转换为数组 pub fn toArray(self: *Self) ![]T { - var node = self.stackTop; + var node = self.stack_top; var res = try self.mem_allocator.alloc(T, self.size()); std.mem.set(T, res, @as(T, 0)); var i: usize = 0; @@ -97,12 +97,12 @@ pub fn main() !void { inc.PrintUtil.printArray(i32, try stack.toArray()); // 访问栈顶元素 - var top = stack.top(); - std.debug.print("\n栈顶元素 top = {}", .{top}); + var peek = stack.peek(); + std.debug.print("\n栈顶元素 top = {}", .{peek}); // 元素出栈 - top = stack.pop(); - std.debug.print("\n出栈元素 pop = {},出栈后 stack = ", .{top}); + var pop = stack.pop(); + std.debug.print("\n出栈元素 pop = {},出栈后 stack = ", .{pop}); inc.PrintUtil.printArray(i32, try stack.toArray()); // 获取栈的长度