Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Adooobe committed Jan 31, 2025
1 parent 5f99b5f commit 341903a
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 18 deletions.
42 changes: 34 additions & 8 deletions exercises/algorithm/algorithm1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
single linked list merge
This problem requires you to merge two ordered singly linked lists into one ordered singly linked list
*/
// I AM NOT DONE

use std::fmt::{self, Display, Formatter};
use std::ptr::NonNull;
use std::vec::*;
use std::{default, vec::*};

#[derive(Debug)]
struct Node<T> {
Expand Down Expand Up @@ -69,14 +68,41 @@ impl<T> LinkedList<T> {
},
}
}
pub fn merge(list_a:LinkedList<T>,list_b:LinkedList<T>) -> Self
pub fn merge(mut list_a:LinkedList<T>,mut list_b:LinkedList<T>) -> Self where T: PartialOrd + Clone
{
//TODO
Self {
length: 0,
start: None,
end: None,
}
// let common = if list_a.length >= list_b.length {list_a.length} else {list_b.length};
// let lens = list_a.length + list_b.length;
let mut result = LinkedList::new();
let mut i = 0;
let mut j = 0;
let len_a = list_a.length as i32;
let len_b = list_b.length as i32;


while i < len_a && j < len_b {
let a = list_a.get(i).unwrap();
let b = list_b.get(j).unwrap();

if a <= b {
result.add(a.clone());
i += 1;
} else {
result.add(b.clone());
j += 1;
}
};
while i < len_a {
result.add(list_a.get(i).unwrap().clone());
i += 1;
};

while j < len_b {
result.add(list_b.get(j).unwrap().clone());
j += 1;
};

result
}
}

Expand Down
20 changes: 19 additions & 1 deletion exercises/algorithm/algorithm2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
double linked list reverse
This problem requires you to reverse a doubly linked list
*/
// I AM NOT DONE

use std::fmt::{self, Display, Formatter};
use std::ptr::NonNull;
Expand Down Expand Up @@ -74,6 +73,25 @@ impl<T> LinkedList<T> {
}
pub fn reverse(&mut self){
// TODO
let mut last_node = self.end;
self.start = last_node;
while let Some(mut current_node) = last_node {

let prev_node = unsafe {(*current_node.as_ptr()).next};
let next_node = unsafe {(*current_node.as_ptr()).prev};
// let prev_prev_node = unsafe {(*prev_node.unwrap().as_ptr()).prev};
unsafe {
(*current_node.as_ptr()).next = next_node;
(*current_node.as_ptr()).prev = prev_node;
last_node = next_node;
}
if let None = next_node {
self.end = last_node;
break;
}
};


}
}

Expand Down
16 changes: 13 additions & 3 deletions exercises/algorithm/algorithm3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
This problem requires you to implement a sorting algorithm
you can use bubble sorting, insertion sorting, heap sorting, etc.
*/
// I AM NOT DONE

fn sort<T>(array: &mut [T]){
//TODO
use std::mem::swap;

fn sort<T>(array: &mut [T]) where T: PartialEq + PartialOrd{
for i in 0..array.len() {
for j in i..array.len() {
if array[i] > array[j] {
array.swap(i, j);
}
}
}
}



#[cfg(test)]
mod tests {
use super::*;
Expand Down
55 changes: 49 additions & 6 deletions exercises/algorithm/algorithm4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
This problem requires you to implement a basic interface for a binary tree
*/

//I AM NOT DONE
use std::cmp::Ordering;
use std::fmt::Debug;
use std::{cmp::Ordering, fmt::Debug};


#[derive(Debug)]
Expand Down Expand Up @@ -50,13 +48,19 @@ where

// Insert a value into the BST
fn insert(&mut self, value: T) {
//TODO
match &mut self.root {
Some(node) => node.insert(value),
None => self.root = Some(Box::new(TreeNode::new(value))),
}
}

// Search for a value in the BST
fn search(&self, value: T) -> bool {
//TODO
true
match &self.root {
Some(node) => node.search(&value),
_ => false
}

}
}

Expand All @@ -67,6 +71,45 @@ where
// Insert a node into the tree
fn insert(&mut self, value: T) {
//TODO
match value.cmp(&self.value) {
Ordering::Less => {
if let Some(left) = &mut self.left {
left.insert(value);
} else {
self.left = Some(Box::new(TreeNode::new(value)));
}
}
Ordering::Greater => {
if let Some(right) = &mut self.right {
right.insert(value);
} else {
self.right = Some(Box::new(TreeNode::new(value)));
}
}
Ordering::Equal => {
// 重复值不插入
}
}
}

fn search(&self, target: &T) -> bool { // 使用引用避免所有权转移
match target.cmp(&self.value) {
Ordering::Equal => true, // 找到匹配值
Ordering::Less => { // 搜索左子树
if let Some(left) = &self.left {
left.search(target)
} else {
false
}
}
Ordering::Greater => { // 搜索右子树
if let Some(right) = &self.right {
right.search(target)
} else {
false
}
}
}
}
}

Expand Down

0 comments on commit 341903a

Please sign in to comment.