Skip to content

Commit

Permalink
algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
guimo3 committed Nov 30, 2024
1 parent a1ecf6b commit f818744
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 45 deletions.
57 changes: 44 additions & 13 deletions exercises/algorithm/algorithm1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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;
Expand Down Expand Up @@ -70,24 +70,55 @@ impl<T> LinkedList<T> {
},
}
}
pub fn merge(list_a:LinkedList<T>,list_b:LinkedList<T>) -> Self
{
//TODO
let mut list_c = LinkedList::<T>::new();

}

// 为 merge 方法添加 Ord 和 Clone 约束
impl<T: Ord + Clone> LinkedList<T> {
pub fn merge(list_a: Self, list_b: Self) -> Self {
let mut list_c = Self::new();
let mut a_ptr = list_a.start;
let mut b_ptr = list_b.start;


// Self {
// length: 0,
// start: None,
// end: None,
// }
// 同时遍历两个链表,比较节点的值,将较小的节点添加到新链表 list_c 中
while let (Some(a_next_ptr), Some(b_next_ptr)) = (a_ptr, b_ptr) {
unsafe {
let a_node = &*a_next_ptr.as_ptr();
let b_node = &*b_next_ptr.as_ptr();

if a_node.val < b_node.val {
list_c.add(a_node.val.clone());
a_ptr = a_node.next;
} else {
list_c.add(b_node.val.clone());
b_ptr = b_node.next;
}
}
}


}
// 如果链表 a 还有剩余节点,将其全部添加到新链表 list_c 中
while let Some(a_next_ptr) = a_ptr {
unsafe {
let a_node = &*a_next_ptr.as_ptr();
list_c.add(a_node.val.clone());
a_ptr = a_node.next;
}
}

// 如果链表 b 还有剩余节点,将其全部添加到新链表 list_c 中
while let Some(b_next_ptr) = b_ptr {
unsafe {
let b_node = &*b_next_ptr.as_ptr();
list_c.add(b_node.val.clone());
b_ptr = b_node.next;
}
}

list_c
}
}


impl<T> Display for LinkedList<T>
where
T: Display,
Expand Down
37 changes: 27 additions & 10 deletions exercises/algorithm/algorithm10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
graph
This problem requires you to implement a basic graph functio
*/
// I AM NOT DONE


use std::collections::{HashMap, HashSet};
use std::fmt;
Expand All @@ -28,20 +28,27 @@ impl Graph for UndirectedGraph {
fn adjacency_table(&self) -> &HashMap<String, Vec<(String, i32)>> {
&self.adjacency_table
}
fn add_edge(&mut self, edge: (&str, &str, i32)) {
//TODO
}
}
pub trait Graph {
fn new() -> Self;
fn adjacency_table_mutable(&mut self) -> &mut HashMap<String, Vec<(String, i32)>>;
fn adjacency_table(&self) -> &HashMap<String, Vec<(String, i32)>>;
fn add_node(&mut self, node: &str) -> bool {
//TODO
true
if !self.contains(node) {
self.adjacency_table_mutable().insert(node.to_string(), Vec::new());
true
} else {
false
}
}
fn add_edge(&mut self, edge: (&str, &str, i32)) {
//TODO
let (from, to, weight) = edge;
if !self.contains(from) {
self.add_node(from);
}
if !self.contains(to) {
self.add_node(to);
}
self.adjacency_table.get_mut(from).unwrap().push((String::from(to), weight));
self.adjacency_table.get_mut(to).unwrap().push((String::from(from), weight));

}
fn contains(&self, node: &str) -> bool {
self.adjacency_table().get(node).is_some()
Expand All @@ -59,6 +66,16 @@ pub trait Graph {
edges
}
}
pub trait Graph {
fn new() -> Self;
fn adjacency_table_mutable(&mut self) -> &mut HashMap<String, Vec<(String, i32)>>;
fn adjacency_table(&self) -> &HashMap<String, Vec<(String, i32)>>;
fn add_node(&mut self, node: &str) -> bool;
fn add_edge(&mut self, edge: (&str, &str, i32));
fn contains(&self, node: &str) -> bool;
fn nodes(&self) -> HashSet<&String>;
fn edges(&self) -> Vec<(&String, &String, i32)>;
}
#[cfg(test)]
mod test_undirected_graph {
use super::Graph;
Expand Down
12 changes: 11 additions & 1 deletion exercises/algorithm/algorithm2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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 +74,16 @@ impl<T> LinkedList<T> {
}
pub fn reverse(&mut self){
// TODO
let mut curr = self.start;
let mut prev = None;
let mut next = None;
while curr.is_some(){
next = unsafe {(*curr.unwrap().as_ptr()).next};
unsafe {(*curr.unwrap().as_ptr()).next = prev};
prev = curr;
curr = next;
}
self.start = prev;
}
}

Expand Down
12 changes: 10 additions & 2 deletions exercises/algorithm/algorithm3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
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]){

fn sort<T: Ord>(array: &mut [T]){
//TODO
let n = array.len();
for i in 0..n {
for j in 0..(n - i -1) {
if array[j] > array[j + 1] {
array.swap(j, j + 1);
}
}
}
}
#[cfg(test)]
mod tests {
Expand Down
41 changes: 36 additions & 5 deletions exercises/algorithm/algorithm4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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;

Expand Down Expand Up @@ -37,6 +36,28 @@ where
right: None,
}
}

// Insert a node into the tree
fn insert(&mut self, value: T) {
//TODO
match self.value.cmp(&value) {
Ordering::Equal => (),
Ordering::Less => {
if let Some(ref mut left) = self.left {
left.insert(value);
} else {
self.left = Some(Box::new(TreeNode::new(value)));
}
}
Ordering::Greater => {
if let Some(ref mut right) = self.right {
right.insert(value);
} else {
self.right = Some(Box::new(TreeNode::new(value)));
}
}
}
}
}

impl<T> BinarySearchTree<T>
Expand All @@ -51,23 +72,33 @@ where
// Insert a value into the BST
fn insert(&mut self, value: T) {
//TODO
if let Some(ref mut root) = self.root {
root.insert(value);
} else {
self.root = Some(Box::new(TreeNode::new(value)));
}
}

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

impl<T> TreeNode<T>
where
T: Ord,
{
// Insert a node into the tree
fn insert(&mut self, value: T) {
fn search(&self, value: T) -> bool {
//TODO
match self.value.cmp(&value) {
Ordering::Equal => true,
Ordering::Less => self.left.as_ref().map_or(false, |node| node.search(value)),
Ordering::Greater => self.right.as_ref().map_or(false, |node| node.search(value)),
}
}

}


Expand Down Expand Up @@ -121,6 +152,6 @@ mod tests {
None => panic!("Root should not be None after insertion"),
}
}
}
}


20 changes: 17 additions & 3 deletions exercises/algorithm/algorithm5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
This problem requires you to implement a basic BFS algorithm
*/

//I AM NOT DONE
use std::collections::VecDeque;

use std::collections::HashSet;
// Define a graph
struct Graph {
adj: Vec<Vec<usize>>,
Expand All @@ -29,8 +28,23 @@ impl Graph {
fn bfs_with_return(&self, start: usize) -> Vec<usize> {

//TODO
let mut queue = VecDeque::new();
let mut visited = HashSet::new();
let mut visit_order = Vec::new();

queue.push_back(start);
visited.insert(start);
while let Some(current) = queue.pop_front() {
visit_order.push(current);
for &neighbor in &self.adj[current] {
if !visited.contains(&neighbor) {
visited.insert(neighbor);
queue.push_back(neighbor);
}
}
}


let mut visit_order = vec![];
visit_order
}
}
Expand Down
10 changes: 9 additions & 1 deletion exercises/algorithm/algorithm6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This problem requires you to implement a basic DFS traversal
*/

// I AM NOT DONE

use std::collections::HashSet;

struct Graph {
Expand All @@ -24,6 +24,14 @@ impl Graph {

fn dfs_util(&self, v: usize, visited: &mut HashSet<usize>, visit_order: &mut Vec<usize>) {
//TODO
visited.insert(v);
visit_order.push(v);

for &neighbor in &self.adj[v] {
if !visited.contains(&neighbor) {
self.dfs_util(neighbor, visited, visit_order);
}
}
}

// Perform a depth-first search on the graph, return the order of visited nodes
Expand Down
28 changes: 25 additions & 3 deletions exercises/algorithm/algorithm7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
This question requires you to use a stack to achieve a bracket match
*/

// I AM NOT DONE
#[derive(Debug)]
struct Stack<T> {
size: usize,
Expand Down Expand Up @@ -32,7 +31,12 @@ impl<T> Stack<T> {
}
fn pop(&mut self) -> Option<T> {
// TODO
None
if 0 == self.size {
return None;
} else {
self.size -= 1;
self.data.pop()
}
}
fn peek(&self) -> Option<&T> {
if 0 == self.size {
Expand Down Expand Up @@ -102,7 +106,25 @@ impl<'a, T> Iterator for IterMut<'a, T> {
fn bracket_match(bracket: &str) -> bool
{
//TODO
true
let mut stack = Stack::new();
for c in bracket.chars() {
match c {
'(' => stack.push(')'),
'[' => stack.push(']'),
'{' => stack.push('}'),
')' | ']' | '}' => {
if let Some(expected) = stack.pop() {
if c!= expected {
return false;
}
} else {
return false;
}
}
_ => continue,
}
}
stack.is_empty()
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit f818744

Please sign in to comment.