data_structure 0.1.20

A Rust library for implementing and managing common data structures.
Documentation
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
use crate::linear::List;  // 确保 `List` 在当前作用域

/// 静态线性表的顺序存储结构
///
/// # 示例
///
/// ```
/// use data_structure::linear::array::SqList;
/// use data_structure::linear::List;
/// let list: SqList<i32> = SqList::init_list();
/// ```
#[derive(Clone, Debug)]
pub struct SqList<T>
where T:PartialEq+Eq
{
    element:[Option<T>;100],
    length:usize, //位序
}

impl<T> PartialEq for SqList<T> where T: PartialEq + Eq {
    fn eq(&self, other: &Self) -> bool {
        self.length == other.length &&
            self.element.iter().take(self.length).eq(other.element.iter().take(other.length))
    }
}

impl<T> List<T> for SqList<T>
where T: Clone + Eq + std::fmt::Debug
{
    /// 初始化一个空的线性表
    ///
    /// # 返回值
    ///
    /// 返回一个新的空 `SqList` 实例
    ///
    /// # 示例
    ///
    /// ```
    /// use data_structure::linear::array::SqList;
    /// use data_structure::linear::List;
    /// let list: SqList<i32> = SqList::init_list();
    /// ```
    fn init_list() -> Self {
        Self {
            element: std::array::from_fn(|_| None),
            length: 0,
        }
    }

    /// 销毁线性表
    ///
    /// # 参数
    ///
    /// * `self` - 要销毁的线性表
    ///
    /// # 示例
    ///
    /// ```
    /// use data_structure::linear::array::SqList;
    /// use data_structure::linear::List;
    /// let list: SqList<i32> = SqList::init_list();
    /// list.destroy_list();
    /// ```
    fn destroy_list(self) {
        drop(self)
    }

    /// 清空线性表中的所有元素
    ///
    /// # 参数
    ///
    /// * `&mut self` - 对线性表的可变引用
    ///
    /// # 示例
    ///
    /// ```
    /// use data_structure::linear::array::SqList;
    /// use data_structure::linear::List;
    /// let mut list: SqList<i32> = SqList::init_list();
    /// list.list_insert(1, 42).unwrap();
    /// list.clear_list();
    /// ```
    fn clear_list(&mut self){
        for elem in self.element.iter_mut().take(self.length) {
            *elem=None;
        }
        self.length=0;
    }

    /// 检查线性表是否为空
    ///
    /// # 参数
    ///
    /// * `&self` - 对线性表的不可变引用
    ///
    /// # 返回值
    ///
    /// 如果线性表为空返回 `true`,否则返回 `false`
    ///
    /// # 示例
    ///
    /// ```
    /// use data_structure::linear::array::SqList;
    /// use data_structure::linear::List;
    /// let list: SqList<i32> = SqList::init_list();
    /// assert!(list.list_empty());
    /// ```
    fn list_empty(&self) -> bool {
        self.length==0
    }

    /// 获取线性表的当前长度
    ///
    /// # 参数
    ///
    /// * `&self` - 对线性表的不可变引用
    ///
    /// # 返回值
    ///
    /// 返回线性表中元素的数量
    ///
    /// # 示例
    ///
    /// ```
    /// use data_structure::linear::array::SqList;
    /// use data_structure::linear::List;
    /// let mut list: SqList<i32> = SqList::init_list();
    /// list.list_insert(1, 42).unwrap();
    /// assert_eq!(list.list_length(), 1);
    /// ```
    fn list_length(&self) -> usize {
        self.length
    }

    /// 获取线性表中指定位置的元素
    ///
    /// # 参数
    ///
    /// * `&self` - 对线性表的不可变引用
    /// * `i` - 元素的位置(从1开始)
    ///
    /// # 返回值
    ///
    /// 如果位置有效返回 `Some(T)`,否则返回 `None`
    ///
    /// # 示例
    ///
    /// ```
    /// use data_structure::linear::array::SqList;
    /// use data_structure::linear::List;
    /// let mut list: SqList<i32> = SqList::init_list();
    /// list.list_insert(1, 42).unwrap();
    /// assert_eq!(list.get_elem(1), Some(42));
    /// ```
    fn get_elem(&self, i: usize) -> Option<T> {
        if i == 0 || i > self.length {
            return None;
        }
        self.element[i-1].clone()
    }

    /// 查找指定元素在线性表中的位置
    ///
    /// # 参数
    ///
    /// * `&self` - 对线性表的不可变引用
    /// * `e` - 要查找的元素
    ///
    /// # 返回值
    ///
    /// 如果找到返回元素的位置(从1开始),否则返回0
    ///
    /// # 示例
    ///
    /// ```
    /// use data_structure::linear::array::SqList;
    /// use data_structure::linear::List;
    /// let mut list: SqList<i32> = SqList::init_list();
    /// list.list_insert(1, 42).expect("TODO: panic message");
    /// assert_eq!(list.locate_elem(42), 1);
    /// ```
    fn locate_elem(&self, e: T) -> usize {
        for (i,element) in self.element.iter().enumerate().take(self.length) {
            if let Some(value)=element{
                if *value==e{
                    return i+1;
                }
            }
        }
        0
    }

    /// 获取指定元素的前驱元素
    ///
    /// # 参数
    ///
    /// * `&self` - 对线性表的不可变引用
    /// * `cur_e` - 当前元素
    /// * `_pre_e` - 用于输出前驱元素的参数(未使用)
    ///
    /// # 返回值
    ///
    /// 如果存在前驱元素返回 `Some(T)`,否则返回 `None`
    ///
    /// # 示例
    ///
    /// ```
    /// use data_structure::linear::array::SqList;
    /// use data_structure::linear::List;
    /// let mut list: SqList<i32> = SqList::init_list();
    /// list.list_insert(1, 42).expect("TODO: panic message");
    /// list.list_insert(2, 73).expect("TODO: panic message");
    /// assert_eq!(list.prior_elem(73, &42), Some(42));
    /// ```
    fn prior_elem(&self, cur_e: T, _pre_e: &T) ->Option<T> {
        for (i,element) in self.element.iter().enumerate().take(self.length) {
            if i == 0 {
                if let Some(value)=element{
                    if *value==cur_e{
                        return None;
                    }
                }
            } else {
                if let Some(value)=element{
                    if *value==cur_e{
                        return self.element[i-1].clone();
                    }
                }
            }
        }
        None
    }

    /// 获取指定元素的后继元素
    ///
    /// # 参数
    ///
    /// * `&self` - 对线性表的不可变引用
    /// * `cur_e` - 当前元素
    /// * `_next_e` - 用于输出后继元素的参数(未使用)
    ///
    /// # 返回值
    ///
    /// 如果存在后继元素返回 `Some(T)`,否则返回 `None`
    ///
    /// # 示例
    ///
    /// ```
    /// use data_structure::linear::array::SqList;
    /// use data_structure::linear::List;
    /// let mut list: SqList<i32> = SqList::init_list();
    /// list.list_insert(1, 42).expect("TODO: panic message");
    /// list.list_insert(2, 73).expect("TODO: panic message");
    /// assert_eq!(list.next_elem(42, &73), Some(73));
    /// ```
    fn next_elem(&self, cur_e: T, _next_e: &T) -> Option<T> {
        for (i,element) in self.element.iter().enumerate().take(self.length-1) {
            if let Some(value)=element{
                if *value==cur_e{
                    return self.element[i+1].clone();
                }
            }
        }
        None
    }

    /// 在指定位置插入元素
    ///
    /// # 参数
    ///
    /// * `&mut self` - 对线性表的可变引用
    /// * `i` - 插入位置(从1开始)
    /// * `e` - 要插入的元素
    ///
    /// # 返回值
    ///
    /// 如果操作成功返回 `Ok(())`,否则返回错误信息
    ///
    /// # 示例
    ///
    /// ```
    /// use data_structure::linear::array::SqList;
    /// use data_structure::linear::List;
    /// let mut list: SqList<i32> = SqList::init_list();
    /// assert!(list.list_insert(1, 42).is_ok());
    /// ```
    fn list_insert(&mut self, i: usize, e: T)->Result<(),crate::Err> {
        if i == 0 || i > self.length + 1 {
            return Err(crate::Err::IndexErr);
        }
        if self.length >= 100 {
            return Err(crate::Err::FullErr);
        }
        let idx = i - 1;
        for j in (idx..self.length).rev() {
            self.element[j + 1] = self.element[j].clone();
        }
        self.element[idx] = Some(e);
        self.length += 1;
        Ok(())
    }

    /// 删除指定位置的元素
    ///
    /// # 参数
    ///
    /// * `&mut self` - 对线性表的可变引用
    /// * `i` - 删除位置(从1开始)
    ///
    /// # 返回值
    ///
    /// 如果操作成功返回 `Ok(())`,否则返回错误信息
    ///
    /// # 示例
    ///
    /// ```
    /// use data_structure::linear::array::SqList;
    /// use data_structure::linear::List;
    /// let mut list: SqList<i32> = SqList::init_list();
    /// list.list_insert(1, 42).expect("TODO: panic message");
    /// assert!(list.list_delete(1).is_ok());
    /// ```
    fn list_delete(&mut self, i: usize)->Result<(),crate::Err> {
        if i == 0 || i > self.length {
            return Err(crate::Err::IndexErr);
        }
        let idx = i - 1;
        if idx == self.length - 1 {
            self.element[idx] = None;
        } else {
            for j in idx..self.length - 1 {
                self.element[j] = self.element[j + 1].clone();
            }
        }
        self.element[self.length - 1] = None;
        self.length -= 1;
        Ok(())
    }

    /// 遍历并打印线性表中的所有元素
    ///
    /// # 参数
    ///
    /// * `&self` - 对线性表的不可变引用
    ///
    /// # 示例
    ///
    /// ```
    /// use data_structure::linear::array::SqList;
    /// use data_structure::linear::List;
    /// let mut list: SqList<i32> = SqList::init_list();
    /// list.list_insert(1, 42).expect("TODO: panic message");
    /// list.list_insert(2, 73).expect("TODO: panic message");
    /// list.traverse_list();
    /// ```
    fn traverse_list(&self) {
        for i in 0..self.length {
            if let Some(value) = &self.element[i] {
                println!("{:?}", value);
            }
        }
    }
}

pub struct ArrayList<T> {
    pub element: [Option<T>; 100],
    pub length: usize,
}

impl<T> ArrayList<T>
where
    T: Copy + PartialEq + std::fmt::Debug,
{
    /// 创建一个新的空 `ArrayList`
    ///
    /// # 示例
    ///
    /// ```
    /// use data_structure::linear::array::ArrayList;
    /// let list: ArrayList<i32> = ArrayList::new();
    /// ```
    pub fn new() -> Self {
        Self {
            element: [None; 100],
            length: 0,
        }
    }

    /// 获取指定位置的元素
    ///
    /// # 参数
    ///
    /// * `index` - 要获取的元素位置(1-based)
    ///
    /// # 返回值
    ///
    /// `Result<T, &str>` - 成功返回元素,失败返回错误信息
    ///
    /// # 错误
    ///
    /// - 如果索引为0或超出当前长度,返回"数组越界"
    /// - 如果元素不存在,返回"元素不存在!"
    pub fn get_element(&self, index: usize) -> Result<T, &'static str> {
        if index == 0 {
            return Err("数组越界");
        }
        let idx = index - 1;
        if idx >= self.length {
            return Err("数组越界");
        };
        match self.element[idx] {
            Some(value) => Ok(value),
            None => Err("元素不存在!"),
        }
    }

    /// 在指定位置插入元素
    ///
    /// # 参数
    ///
    /// * `position` - 插入位置(1-based)
    /// * `element` - 要插入的元素
    ///
    /// # 返回值
    ///
    /// `Result<(), &str>` - 成功返回`Ok(())`,失败返回错误信息
    ///
    /// # 错误
    ///
    /// - 如果位置无效,返回"数组越界"
    /// - 如果数组已满,返回"数组已满"
    pub fn insert(&mut self, position: usize, element: T) -> Result<(), &'static str> {
        if position == 0 || position > self.length + 1 {
            return Err("数组越界");
        };
        let index = position - 1;
        if self.length >= 100 {
            return Err("数组已满");
        };
        for i in (index..self.length).rev() {
            self.element[i + 1] = self.element[i];
        };
        self.element[index] = Some(element);
        self.length += 1;
        Ok(())
    }

    /// 查找元素的位置
    ///
    /// # 参数
    ///
    /// * `element` - 要查找的元素
    ///
    /// # 返回值
    ///
    /// `Result<usize, &str>` - 成功返回位置(1-based),失败返回"查找失败"
    pub fn locate_index(&self, element: T) -> Result<usize, &'static str> {
        for i in 0..self.length {
            if let Some(value) = self.element[i] {
                if value == element {
                    return Ok(i + 1);
                }
            }
        }
        Err("查找失败")
    }

    /// 删除指定位置的元素
    ///
    /// # 参数
    ///
    /// * `index` - 要删除的位置(1-based)
    ///
    /// # 返回值
    ///
    /// `Result<(), &str>` - 成功返回`Ok(())`,失败返回"数组操作越界!"
    pub fn delete(&mut self, index: usize) -> Result<(), &'static str> {
        if index == 0 || index > self.length {
            return Err("数组操作越界!");
        };
        for index in index..self.length {
            self.element[index - 1] = self.element[index];
        }
        self.element[self.length - 1] = None;
        self.length -= 1;
        Ok(())
    }

    /// 清空列表
    pub fn clear(&mut self) {
        for index in 0..self.length {
            self.element[index] = None;
        };
        self.length = 0;
    }

    /// 获取列表长度
    pub fn length(&self) -> usize {
        self.length
    }

    /// 检查列表是否非空
    ///
    /// # 返回值
    ///
    /// 如果列表不为空返回`true`,否则返回`false`
    pub fn empty(&self) -> bool {
        self.length != 0
    }

    /// 获取元素的前驱
    ///
    /// # 参数
    ///
    /// * `cur_e` - 当前元素
    ///
    /// # 返回值
    ///
    /// `Result<T, &str>` - 成功返回前驱元素,失败返回错误信息
    ///
    /// # 错误
    ///
    /// - 如果列表为空,返回"数组为空"
    /// - 如果是第一个元素,返回"当前元素为第一个元素,没有前驱"
    /// - 如果元素不存在,返回"元素不存在!"
    pub fn prior_element(&self, cur_e: T) -> Result<T, &'static str> {
        if self.empty() {
            return Err("数组为空");
        };
        if self.element[0] == Some(cur_e) {
            return Err("当前元素为第一个元素,没有前驱");
        };
        for index in 1..self.length {
            if self.element[index] == Some(cur_e) {
                return Ok(self.element[index - 1].unwrap());
            };
        };
        Err("元素不存在!")
    }

    /// 获取元素后继
    ///
    /// # 参数
    ///
    /// * `cur_e` - 当前元素
    ///
    /// # 返回值
    ///
    /// `Result<T, &str>` - 成功返回后继元素,失败返回错误信息
    ///
    /// # 错误
    ///
    /// - 如果列表为空,返回"数组为空"
    /// - 如果是最后一个元素,返回"当前元素为最后一个元素,没有后继"
    /// - 如果元素不存在,返回"元素不存在!"
    pub fn next_element(&self, cur_e: T) -> Result<T, &'static str> {
        if self.empty() {
            return Err("数组为空");
        }
        if self.element[self.length - 1] == Some(cur_e) {
            return Err("当前元素为最后一个元素,没有后继");
        }
        for index in 0..self.length - 1 {
            if self.element[index] == Some(cur_e) {
                return Ok(self.element[index + 1].unwrap());
            }
        }
        Err("元素不存在!")
    }

    /// 遍历并打印所有元素
    pub fn traverse(&self) {
        for index in 0..self.length {
            println!("{:?}", self.element[index].unwrap());
        }
    }
}