Skip to content

Commit e420a60

Browse files
committed
linked list un completed
1 parent 404445f commit e420a60

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

Diff for: linked/Cargo.lock

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: linked/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "linked"
3+
version = "0.1.0"
4+
authors = ["chunhui.yao <[email protected]>"]

Diff for: linked/src/main.rs

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
use std::boxed::Box;
2+
enum LinkedListNext {
3+
Next(Box<LinkedNode>),
4+
Nil
5+
}
6+
7+
impl LinkedListNext {
8+
fn new_next(val: i32) -> LinkedListNext {
9+
LinkedListNext::Next(
10+
Box::new(LinkedNode::new(val))
11+
)
12+
}
13+
}
14+
15+
struct LinkedNode {
16+
value: i32,
17+
next: LinkedListNext
18+
}
19+
20+
impl LinkedNode {
21+
fn new(value: i32) -> LinkedNode {
22+
return LinkedNode { value: value, next: LinkedListNext::Nil }
23+
}
24+
25+
fn append(&mut self, val: i32) {
26+
match self.next {
27+
LinkedListNext::Next(ref mut node) => {
28+
node.append(val);
29+
}
30+
LinkedListNext::Nil => {
31+
self.next = LinkedListNext::new_next(val);
32+
}
33+
}
34+
}
35+
36+
fn insert(&mut self, val: i32, depth: i32) {
37+
match self.next {
38+
LinkedListNext::Next(ref mut node) => {
39+
if depth <= 0 {
40+
}
41+
else {
42+
node.insert(val, depth - 1);
43+
}
44+
}
45+
LinkedListNext::Nil => {
46+
return
47+
}
48+
}
49+
50+
self.next = LinkedListNext::Next(
51+
Box::new(LinkedNode { value: val, next: self.next })
52+
);
53+
54+
}
55+
56+
}
57+
58+
struct LinkedList {
59+
head: Box<LinkedNode>,
60+
}
61+
62+
impl LinkedList {
63+
fn new() -> LinkedList {
64+
return LinkedList {
65+
head: Box::new(LinkedNode::new(-1))
66+
}
67+
}
68+
69+
fn output(&self) {
70+
let mut cur_node = &self.head;
71+
print!("Head");
72+
loop {
73+
match cur_node.next {
74+
LinkedListNext::Next(ref node) => {
75+
cur_node = &node;
76+
print!("->{}", cur_node.value);
77+
}
78+
LinkedListNext::Nil => {
79+
println!("");
80+
return
81+
}
82+
}
83+
}
84+
}
85+
86+
fn append(&mut self, val: i32) {
87+
self.head.append(val);
88+
}
89+
}
90+
91+
fn main() {
92+
let mut list = LinkedList::new();
93+
list.append(2);
94+
list.append(3);
95+
list.append(4);
96+
list.append(5);
97+
list.output();
98+
println!("Hello, world!");
99+
}

0 commit comments

Comments
 (0)