-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdoubly_linked_list.rs
More file actions
151 lines (128 loc) · 4.02 KB
/
Copy pathdoubly_linked_list.rs
File metadata and controls
151 lines (128 loc) · 4.02 KB
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
151
use std::cell::RefCell;
use std::fmt::{Debug, Formatter, Result};
use std::rc::Rc;
type Link<T> = Rc<RefCell<Node<T>>>;
#[derive(Debug)]
pub struct DoublyLinkedList<T> {
pub head: Option<Link<T>>,
pub tail: Option<Link<T>>,
}
pub struct Node<T> {
pub data: T,
pub prev: Option<Link<T>>,
next: Option<Link<T>>,
}
impl<T: Debug> DoublyLinkedList<T> {
pub fn new() -> Self {
Self {
head: None,
tail: None,
}
}
pub fn push_end(&mut self, value: T) {
let link = Node::new(value);
match self.tail.take() {
Some(old_tail) => {
old_tail.borrow_mut().next = Some(Rc::clone(&link));
link.borrow_mut().prev = Some(old_tail);
}
None => {
self.head = Some(Rc::clone(&link));
}
}
self.tail = Some(link);
}
pub fn pop_front(&mut self) -> Option<Node<T>> {
self.head.take().map(|old_head| {
match old_head.borrow_mut().next.take() {
Some(next) => {
next.borrow_mut().prev = None;
self.head = Some(next);
}
None => {
self.tail = None;
}
}
Rc::try_unwrap(old_head)
.expect("RefCell contained in Rc")
.into_inner()
})
}
}
impl<T: Debug> Node<T> {
pub fn new(data: T) -> Link<T> {
Rc::new(RefCell::new(Self {
data,
prev: None,
next: None,
}))
}
}
impl<T: Debug> Debug for Node<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.debug_struct("Node")
.field("data", &self.data)
.field("prev", &self.prev.as_ref().map(|_| "Node"))
.field("next", &self.next.as_ref().map(|_| "Node"))
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_node_new() {
let node = Node::new(11);
assert_eq!(node.borrow().data, 11);
assert!(node.borrow().next.is_none());
assert!(node.borrow().prev.is_none());
}
#[test]
fn test_list_empty() {
let empty = DoublyLinkedList::<i32>::new();
assert!(empty.head.is_none());
assert!(empty.tail.is_none());
}
#[test]
fn test_list_push_end() {
let mut list = DoublyLinkedList::<i32>::new();
list.push_end(11);
let head = list.head.as_ref().unwrap();
assert_eq!(head.borrow().data, 11);
assert!(head.borrow().prev.is_none());
assert!(head.borrow().next.is_none());
let tail = list.tail.as_ref().unwrap();
assert_eq!(tail.borrow().data, 11);
assert!(tail.borrow().prev.is_none());
assert!(tail.borrow().next.is_none());
list.push_end(22);
let head = list.head.as_ref().unwrap();
assert_eq!(head.borrow().data, 11);
assert!(head.borrow().prev.is_none());
assert!(head.borrow().next.is_some());
let tail = list.tail.as_ref().unwrap();
assert_eq!(tail.borrow().data, 22);
assert!(tail.borrow().prev.is_some());
assert!(tail.borrow().next.is_none());
}
#[test]
fn test_list_pop_front() {
let mut list = DoublyLinkedList::<i32>::new();
assert!(list.pop_front().is_none());
list.push_end(13);
list.push_end(14);
let node = list.pop_front().expect("the node 13 is in place");
assert_eq!(node.data, 13);
let head = list.head.as_ref().unwrap();
assert_eq!(head.borrow().data, 14);
assert!(head.borrow().prev.is_none());
assert!(head.borrow().next.is_none());
let tail = list.tail.as_ref().unwrap();
assert_eq!(tail.borrow().data, 14);
assert!(tail.borrow().prev.is_none());
assert!(tail.borrow().next.is_none());
let node = list.pop_front().expect("the node 14 is in place");
assert_eq!(node.data, 14);
assert!(list.pop_front().is_none());
}
}