File tree 4 files changed +29
-26
lines changed
chapter_array_and_linkedlist
4 files changed +29
-26
lines changed Original file line number Diff line number Diff line change @@ -19,8 +19,7 @@ struct MyList {
19
19
impl MyList {
20
20
/* 构造方法 */
21
21
pub fn new ( capacity : usize ) -> Self {
22
- let mut vec = Vec :: new ( ) ;
23
- vec. resize ( capacity, 0 ) ;
22
+ let mut vec = vec ! [ 0 ; capacity] ;
24
23
Self {
25
24
arr : vec,
26
25
capacity,
@@ -92,7 +91,7 @@ impl MyList {
92
91
} ;
93
92
let num = self . arr [ index] ;
94
93
// 将将索引 index 之后的元素都向前移动一位
95
- for j in ( index..self . size - 1 ) {
94
+ for j in index..self . size - 1 {
96
95
self . arr [ j] = self . arr [ j + 1 ] ;
97
96
}
98
97
// 更新元素数量
@@ -111,7 +110,7 @@ impl MyList {
111
110
}
112
111
113
112
/* 将列表转换为数组 */
114
- pub fn to_array ( & mut self ) -> Vec < i32 > {
113
+ pub fn to_array ( & self ) -> Vec < i32 > {
115
114
// 仅转换有效长度范围内的列表元素
116
115
let mut arr = Vec :: new ( ) ;
117
116
for i in 0 ..self . size {
Original file line number Diff line number Diff line change @@ -120,7 +120,7 @@ impl<T: Copy> LinkedListDeque<T> {
120
120
}
121
121
}
122
122
self . que_size -= 1 ; // 更新队列长度
123
- Rc :: try_unwrap ( old_front) . ok ( ) . unwrap ( ) . into_inner ( ) . val
123
+ old_front. borrow ( ) . val
124
124
} )
125
125
}
126
126
// 队尾出队操作
@@ -136,7 +136,7 @@ impl<T: Copy> LinkedListDeque<T> {
136
136
}
137
137
}
138
138
self . que_size -= 1 ; // 更新队列长度
139
- Rc :: try_unwrap ( old_rear) . ok ( ) . unwrap ( ) . into_inner ( ) . val
139
+ old_rear. borrow ( ) . val
140
140
} )
141
141
}
142
142
}
@@ -163,12 +163,16 @@ impl<T: Copy> LinkedListDeque<T> {
163
163
164
164
/* 返回数组用于打印 */
165
165
pub fn to_array ( & self , head : Option < & Rc < RefCell < ListNode < T > > > > ) -> Vec < T > {
166
- if let Some ( node) = head {
167
- let mut nums = self . to_array ( node. borrow ( ) . next . as_ref ( ) ) ;
168
- nums. insert ( 0 , node. borrow ( ) . val ) ;
169
- return nums;
166
+ let mut res: Vec < T > = Vec :: new ( ) ;
167
+ fn recur < T : Copy > ( cur : Option < & Rc < RefCell < ListNode < T > > > > , res : & mut Vec < T > ) {
168
+ if let Some ( cur) = cur {
169
+ res. push ( cur. borrow ( ) . val ) ;
170
+ recur ( cur. borrow ( ) . next . as_ref ( ) , res) ;
171
+ }
170
172
}
171
- return Vec :: new ( ) ;
173
+
174
+ recur ( head, & mut res) ;
175
+ res
172
176
}
173
177
}
174
178
Original file line number Diff line number Diff line change @@ -67,7 +67,7 @@ impl<T: Copy> LinkedListQueue<T> {
67
67
}
68
68
}
69
69
self . que_size -= 1 ;
70
- Rc :: try_unwrap ( old_front) . ok ( ) . unwrap ( ) . into_inner ( ) . val
70
+ old_front. borrow ( ) . val
71
71
} )
72
72
}
73
73
@@ -78,12 +78,18 @@ impl<T: Copy> LinkedListQueue<T> {
78
78
79
79
/* 将链表转化为 Array 并返回 */
80
80
pub fn to_array ( & self , head : Option < & Rc < RefCell < ListNode < T > > > > ) -> Vec < T > {
81
- if let Some ( node) = head {
82
- let mut nums = self . to_array ( node. borrow ( ) . next . as_ref ( ) ) ;
83
- nums. insert ( 0 , node. borrow ( ) . val ) ;
84
- return nums;
81
+ let mut res: Vec < T > = Vec :: new ( ) ;
82
+
83
+ fn recur < T : Copy > ( cur : Option < & Rc < RefCell < ListNode < T > > > > , res : & mut Vec < T > ) {
84
+ if let Some ( cur) = cur {
85
+ res. push ( cur. borrow ( ) . val ) ;
86
+ recur ( cur. borrow ( ) . next . as_ref ( ) , res) ;
87
+ }
85
88
}
86
- return Vec :: new ( ) ;
89
+
90
+ recur ( head, & mut res) ;
91
+
92
+ res
87
93
}
88
94
}
89
95
Original file line number Diff line number Diff line change @@ -45,16 +45,10 @@ impl<T: Copy> LinkedListStack<T> {
45
45
/* 出栈 */
46
46
pub fn pop ( & mut self ) -> Option < T > {
47
47
self . stack_peek . take ( ) . map ( |old_head| {
48
- match old_head. borrow_mut ( ) . next . take ( ) {
49
- Some ( new_head) => {
50
- self . stack_peek = Some ( new_head) ;
51
- }
52
- None => {
53
- self . stack_peek = None ;
54
- }
55
- }
48
+ self . stack_peek = old_head. borrow_mut ( ) . next . take ( ) ;
56
49
self . stk_size -= 1 ;
57
- Rc :: try_unwrap ( old_head) . ok ( ) . unwrap ( ) . into_inner ( ) . val
50
+
51
+ old_head. borrow ( ) . val
58
52
} )
59
53
}
60
54
You can’t perform that action at this time.
0 commit comments