Skip to content

Commit 892bdf8

Browse files
authored
Merge pull request #1 from dirk122119/feature/singlylinklist
Feature/singlylinklist
2 parents cb35a9c + 6c01c44 commit 892bdf8

File tree

116 files changed

+362
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+362
-0
lines changed

DSA/link-list/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DSA/link-list/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "link-list"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

DSA/link-list/readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Link List
2+
### ready to understand
3+
- [] ref mut self

DSA/link-list/src/lib.rs

Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
#[derive(Debug)]
2+
struct Node<T> {
3+
val: T,
4+
next: Option<Box<Node<T>>>,
5+
}
6+
7+
#[derive(Debug)]
8+
struct MyLinkedList<T> {
9+
head: Option<Box<Node<T>>>,
10+
}
11+
12+
#[warn(unused_mut)]
13+
impl<T> Node<T> {
14+
fn new(val: T) -> Box<Node<T>> {
15+
Box::new(Node {
16+
val: val,
17+
next: None,
18+
})
19+
}
20+
}
21+
#[warn(unused_mut)]
22+
impl<T: std::fmt::Debug + Clone + Copy> MyLinkedList<T> {
23+
24+
fn new() -> Self {
25+
MyLinkedList {
26+
head: None,
27+
}
28+
}
29+
30+
fn get(&self, index: i32) -> Option<T> {
31+
let mut current = &self.head;
32+
let mut i = 0;
33+
while let Some(ref node) = current {
34+
if i == index {
35+
return Some(node.val.clone());
36+
}
37+
current = &node.next;
38+
i += 1;
39+
}
40+
None
41+
}
42+
43+
44+
fn add_at_head(&mut self, val: T) {
45+
let new_node = Box::new(Node{ val, next: self.head.take() });
46+
self.head = Some(new_node);
47+
}
48+
49+
fn add_at_tail(&mut self, val: T) {
50+
let new_node = Node::new(val);
51+
match self.head {
52+
None => {
53+
self.head = Some(new_node);
54+
return;
55+
},
56+
Some(ref mut current) => {
57+
let mut current = current.as_mut();
58+
while let Some(ref mut next_node) = current.next {
59+
current = next_node;
60+
}
61+
current.next = Some(new_node);
62+
}
63+
}
64+
}
65+
66+
fn add_at_index(&mut self, index: i32, val: T) {
67+
if index == 0 {
68+
self.add_at_head(val);
69+
return;
70+
}
71+
let mut i = 1;
72+
let mut current = match self.head.as_mut(){
73+
Some(node) => node,
74+
None => return,
75+
};
76+
while i< index {
77+
match current.next.as_mut() {
78+
Some(next_node) => {
79+
current = next_node;
80+
i += 1;
81+
}
82+
None => return,
83+
}
84+
}
85+
current.next = Some(Box::new(Node{val, next: current.next.take()}));
86+
}
87+
fn delete_at_index(&mut self, index: i32) {
88+
if index == 0 {
89+
match self.head.take() {
90+
Some(node) => self.head = node.next,
91+
None => return,
92+
}
93+
return;
94+
}
95+
let mut i = 1;
96+
let mut current = match self.head.as_mut(){
97+
Some(node) => node,
98+
None => return,
99+
};
100+
while i< index {
101+
match current.next.as_mut() {
102+
Some(next_node) => {
103+
current = next_node;
104+
i += 1;
105+
}
106+
None => return,
107+
}
108+
}
109+
let mut next_node = match current.next.take() {
110+
Some(next_node) =>next_node.next,
111+
None => return,
112+
};
113+
current.next = next_node;
114+
115+
}
116+
}
117+
118+
119+
120+
121+
pub fn add(left: usize, right: usize) -> usize {
122+
left + right
123+
}
124+
125+
#[cfg(test)]
126+
mod tests {
127+
use super::*;
128+
129+
#[test]
130+
fn it_works() {
131+
let result = add(2, 2);
132+
assert_eq!(result, 4);
133+
}
134+
135+
#[test]
136+
fn test_init_linklist() {
137+
let mut list:MyLinkedList<i32> = MyLinkedList::new();
138+
assert_eq!(list.head.is_none(), true);
139+
}
140+
141+
#[test]
142+
fn test_add_at_head() {
143+
let mut list:MyLinkedList<i32> = MyLinkedList::new();
144+
list.add_at_head(1);
145+
list.add_at_head(2);
146+
147+
let mut val_vec = vec![];
148+
let mut current = &list.head;
149+
while let Some(ref node) = current {
150+
val_vec.push(node.val);
151+
current = &node.next;
152+
}
153+
assert_eq!(val_vec, [2,1]);
154+
}
155+
156+
#[test]
157+
fn test_add_at_tail() {
158+
let mut list:MyLinkedList<i32> = MyLinkedList::new();
159+
list.add_at_tail(1);
160+
list.add_at_tail(2);
161+
162+
let mut val_vec = vec![];
163+
let mut current = &list.head;
164+
while let Some(ref node) = current {
165+
val_vec.push(node.val);
166+
current = &node.next;
167+
}
168+
assert_eq!(val_vec, [1,2]);
169+
}
170+
171+
#[test]
172+
fn test_add_at_index_0() {
173+
let mut list:MyLinkedList<i32> = MyLinkedList::new();
174+
list.add_at_index(0, 1);
175+
list.add_at_index(0, 2);
176+
list.add_at_index(0, 3);
177+
178+
let mut val_vec = vec![];
179+
let mut current = &list.head;
180+
while let Some(ref node) = current {
181+
val_vec.push(node.val);
182+
current = &node.next;
183+
}
184+
println!("{:?}", list);
185+
assert_eq!(val_vec, [3,2,1]);
186+
}
187+
188+
#[test]
189+
fn test_add_at_index_1() {
190+
let mut list:MyLinkedList<i32> = MyLinkedList::new();
191+
192+
list.add_at_index(0, 1);
193+
list.add_at_index(1, 2);
194+
list.add_at_index(1, 3);
195+
list.add_at_index(1, 1);
196+
197+
let mut val_vec = vec![];
198+
let mut current = &list.head;
199+
while let Some(ref node) = current {
200+
val_vec.push(node.val);
201+
current = &node.next;
202+
}
203+
println!("{:?}", list);
204+
assert_eq!(val_vec, [1,1,3,2]);
205+
}
206+
#[test]
207+
fn test_delete_at_index_0_in_none_node() {
208+
let mut list:MyLinkedList<i32> = MyLinkedList::new();
209+
list.delete_at_index(0);
210+
211+
let mut val_vec = vec![];
212+
let mut current = &list.head;
213+
while let Some(ref node) = current {
214+
val_vec.push(node.val);
215+
current = &node.next;
216+
}
217+
println!("{:?}", list);
218+
assert_eq!(val_vec, []);
219+
}
220+
221+
#[test]
222+
fn test_delete_at_index_0() {
223+
let mut list:MyLinkedList<i32> = MyLinkedList::new();
224+
list.add_at_index(0, 1);
225+
list.add_at_index(0, 2);
226+
list.add_at_index(0, 3);
227+
list.delete_at_index(0);
228+
229+
let mut val_vec = vec![];
230+
let mut current = &list.head;
231+
while let Some(ref node) = current {
232+
val_vec.push(node.val);
233+
current = &node.next;
234+
}
235+
println!("{:?}", list);
236+
assert_eq!(val_vec, [2,1]);
237+
}
238+
239+
#[test]
240+
fn test_delete_at_index_1() {
241+
let mut list:MyLinkedList<i32> = MyLinkedList::new();
242+
list.add_at_index(0, 1);
243+
list.add_at_index(0, 2);
244+
list.add_at_index(0, 3);
245+
list.delete_at_index(1);
246+
247+
let mut val_vec = vec![];
248+
let mut current = &list.head;
249+
while let Some(ref node) = current {
250+
val_vec.push(node.val);
251+
current = &node.next;
252+
}
253+
println!("{:?}", list);
254+
assert_eq!(val_vec, [3,1]);
255+
}
256+
#[test]
257+
fn test_delete_at_index_2() {
258+
let mut list:MyLinkedList<i32> = MyLinkedList::new();
259+
list.add_at_index(0, 1);
260+
list.add_at_index(0, 2);
261+
list.add_at_index(0, 3);
262+
list.add_at_index(0, 4);
263+
list.delete_at_index(2);
264+
265+
let mut val_vec = vec![];
266+
let mut current = &list.head;
267+
while let Some(ref node) = current {
268+
val_vec.push(node.val);
269+
current = &node.next;
270+
}
271+
println!("{:?}", list);
272+
assert_eq!(val_vec, [4,3,1]);
273+
}
274+
275+
#[test]
276+
fn test_get() {
277+
let mut list:MyLinkedList<i32> = MyLinkedList::new();
278+
list.add_at_index(0, 1);
279+
list.add_at_index(0, 2);
280+
list.add_at_index(0, 3);
281+
list.add_at_index(0, 4);
282+
list.get(2);
283+
284+
assert_eq!(list.get(2), Some(2));
285+
}
286+
287+
288+
}

DSA/link-list/target/.rustc_info.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc_fingerprint":266065799865256242,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.77.2 (25ef9e3d8 2024-04-09)\nbinary: rustc\ncommit-hash: 25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04\ncommit-date: 2024-04-09\nhost: aarch64-apple-darwin\nrelease: 1.77.2\nLLVM version: 17.0.6\n","stderr":""},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/he8477/.rustup/toolchains/stable-aarch64-apple-darwin\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"aarch64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"aes\"\ntarget_feature=\"crc\"\ntarget_feature=\"dit\"\ntarget_feature=\"dotprod\"\ntarget_feature=\"dpb\"\ntarget_feature=\"dpb2\"\ntarget_feature=\"fcma\"\ntarget_feature=\"fhm\"\ntarget_feature=\"flagm\"\ntarget_feature=\"fp16\"\ntarget_feature=\"frintts\"\ntarget_feature=\"jsconv\"\ntarget_feature=\"lor\"\ntarget_feature=\"lse\"\ntarget_feature=\"neon\"\ntarget_feature=\"paca\"\ntarget_feature=\"pacg\"\ntarget_feature=\"pan\"\ntarget_feature=\"pmuv3\"\ntarget_feature=\"ras\"\ntarget_feature=\"rcpc\"\ntarget_feature=\"rcpc2\"\ntarget_feature=\"rdm\"\ntarget_feature=\"sb\"\ntarget_feature=\"sha2\"\ntarget_feature=\"sha3\"\ntarget_feature=\"ssbs\"\ntarget_feature=\"vh\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n","stderr":""}},"successes":{}}

DSA/link-list/target/CACHEDIR.TAG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Signature: 8a477f597d28d172789f06886806bc55
2+
# This file is a cache directory tag created by cargo.
3+
# For information about cache directory tags see https://bford.info/cachedir/

DSA/link-list/target/debug/.cargo-lock

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"$message_type":"diagnostic","message":"variable does not need to be mutable","code":{"code":"unused_mut","explanation":null},"level":"warning","spans":[{"file_name":"src/lib.rs","byte_start":2740,"byte_end":2753,"line_start":109,"line_end":109,"column_start":13,"column_end":26,"is_primary":true,"text":[{"text":" let mut next_node = match current.next.take() {","highlight_start":13,"highlight_end":26}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"the lint level is defined here","code":null,"level":"note","spans":[{"file_name":"src/lib.rs","byte_start":329,"byte_end":339,"line_start":21,"line_end":21,"column_start":8,"column_end":18,"is_primary":true,"text":[{"text":"#[warn(unused_mut)]","highlight_start":8,"highlight_end":18}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":null},{"message":"remove this `mut`","code":null,"level":"help","spans":[{"file_name":"src/lib.rs","byte_start":2740,"byte_end":2744,"line_start":109,"line_end":109,"column_start":13,"column_end":17,"is_primary":true,"text":[{"text":" let mut next_node = match current.next.take() {","highlight_start":13,"highlight_end":17}],"label":null,"suggested_replacement":"","suggestion_applicability":"MachineApplicable","expansion":null}],"children":[],"rendered":null}],"rendered":"\u001b[0m\u001b[1m\u001b[33mwarning\u001b[0m\u001b[0m\u001b[1m: variable does not need to be mutable\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/lib.rs:109:13\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m109\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m let mut next_node = match current.next.take() {\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m----\u001b[0m\u001b[0m\u001b[1m\u001b[33m^^^^^^^^^\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12mhelp: remove this `mut`\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;10mnote\u001b[0m\u001b[0m: the lint level is defined here\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/lib.rs:21:8\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m21\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m#[warn(unused_mut)]\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;10m^^^^^^^^^^\u001b[0m\n\n"}
2+
{"$message_type":"diagnostic","message":"variable does not need to be mutable","code":{"code":"unused_mut","explanation":null},"level":"warning","spans":[{"file_name":"src/lib.rs","byte_start":3185,"byte_end":3193,"line_start":137,"line_end":137,"column_start":13,"column_end":21,"is_primary":true,"text":[{"text":" let mut list:MyLinkedList<i32> = MyLinkedList::new();","highlight_start":13,"highlight_end":21}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"`#[warn(unused_mut)]` on by default","code":null,"level":"note","spans":[],"children":[],"rendered":null},{"message":"remove this `mut`","code":null,"level":"help","spans":[{"file_name":"src/lib.rs","byte_start":3185,"byte_end":3189,"line_start":137,"line_end":137,"column_start":13,"column_end":17,"is_primary":true,"text":[{"text":" let mut list:MyLinkedList<i32> = MyLinkedList::new();","highlight_start":13,"highlight_end":17}],"label":null,"suggested_replacement":"","suggestion_applicability":"MachineApplicable","expansion":null}],"children":[],"rendered":null}],"rendered":"\u001b[0m\u001b[1m\u001b[33mwarning\u001b[0m\u001b[0m\u001b[1m: variable does not need to be mutable\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/lib.rs:137:13\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m137\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m let mut list:MyLinkedList<i32> = MyLinkedList::new();\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m----\u001b[0m\u001b[0m\u001b[1m\u001b[33m^^^^\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12mhelp: remove this `mut`\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m= \u001b[0m\u001b[0m\u001b[1mnote\u001b[0m\u001b[0m: `#[warn(unused_mut)]` on by default\u001b[0m\n\n"}
3+
{"$message_type":"diagnostic","message":"2 warnings emitted","code":null,"level":"warning","spans":[],"children":[],"rendered":"\u001b[0m\u001b[1m\u001b[33mwarning\u001b[0m\u001b[0m\u001b[1m: 2 warnings emitted\u001b[0m\n\n"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
e68ff8cc1156d952
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc":16006274961074835420,"features":"[]","declared_features":"","target":15571544720188531249,"profile":13957322908208868000,"path":17523903030608720598,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/link-list-676a9b0ed483bcfc/dep-test-lib-link-list"}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f0e36d108808a603
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc":16006274961074835420,"features":"[]","declared_features":"","target":15571544720188531249,"profile":14400670141151969058,"path":17523903030608720598,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/link-list-828dc97d2427cddd/dep-lib-link-list"}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}

0 commit comments

Comments
 (0)