1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use std::cell::RefCell;
use std::option::Option;
use std::rc::Rc;
#[derive(Debug, Clone)]
struct Double_Node {
val: i64,
next: Option<Rc<RefCell<Double_Node>>>,
prev: Option<Rc<RefCell<Double_Node>>>,
}
impl Double_Node {
fn new(val: i64) -> Rc<RefCell<Double_Node>> {
return Rc::new(RefCell::new(Double_Node {
val,
next: None,
prev: None,
}));
}
}
pub struct Double_LinkedList {
length: usize,
head: Option<Rc<RefCell<Double_Node>>>,
tail: Option<Rc<RefCell<Double_Node>>>,
}
impl Double_LinkedList {
pub fn new() -> Double_LinkedList {
return Double_LinkedList {
length: 0,
head: None,
tail: None,
};
}
pub fn append(&mut self, val: i64) {
let new_node = Double_Node::new(val);
if self.length == 0 {
self.head = Some(new_node.clone());
self.tail = Some(new_node.clone());
} else {
let tail = self.tail.take().unwrap();
tail.borrow_mut().next = Some(new_node.clone());
new_node.borrow_mut().prev = Some(tail.clone());
self.tail = Some(new_node.clone());
}
self.length += 1;
}
pub fn pop(&mut self) -> Option<i64> {
let tail = self.tail.take();
let mut result = None;
match tail{
Some(node)=>{
result = Some(node.borrow().val);
self.tail = node.borrow().prev.clone();
match self.tail.as_ref(){
Some(tail)=>{
tail.borrow_mut().next=None;
}
None=>{
self.head = None;
}
}
}
None => {
return None;
}
}
result
}
pub fn iter(&self) -> Double_LinkedList_Iterator {
return Double_LinkedList_Iterator::new(self.head.clone());
}
}
pub struct Double_LinkedList_Iterator {
current: Option<Rc<RefCell<Double_Node>>>,
}
impl Double_LinkedList_Iterator {
fn new(current: Option<Rc<RefCell<Double_Node>>>) -> Double_LinkedList_Iterator {
return Double_LinkedList_Iterator { current };
}
}
impl Iterator for Double_LinkedList_Iterator {
type Item = i64;
fn next(&mut self) -> Option<i64> {
let mut result = None;
self.current = match &self.current {
Some(current) => {
let current = current.borrow();
result = Some(current.val);
match current.next.as_ref() {
Some(current) => Some(current.clone()),
None => None,
}
}
None => None,
};
result
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_double_node_new() {
let mut test_double_node = Double_Node::new(10);
}
#[test]
fn test_double_linkedlist_iterator() {
let mut test = Double_LinkedList::new();
test.append(10);
test.append(15);
test.append(19);
test.append(21);
for i in test.iter() {
print!("{:?}\n", i);
}
}
#[test]
fn test_double_linkedlist_pop(){
let mut test = Double_LinkedList::new();
test.append(10);
test.append(15);
test.append(19);
test.append(21);
test.pop();
test.pop();
test.pop();
for i in test.iter() {
print!("{:?}\n", i);
}
}
}