mirror of
https://github.com/Noratrieb/datastructures.git
synced 2026-01-14 17:35:02 +01:00
iter
This commit is contained in:
parent
18f78761c8
commit
1c520dce45
1 changed files with 58 additions and 3 deletions
|
|
@ -1,9 +1,64 @@
|
||||||
pub struct LinkedList {}
|
use std::ptr::NonNull;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct LinkedList<T> {
|
||||||
|
start: Option<NonNull<Node<T>>>,
|
||||||
|
end: Option<NonNull<Node<T>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Clone for LinkedList<T>
|
||||||
|
where
|
||||||
|
T: Clone,
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Node<T> {
|
pub struct Node<T> {
|
||||||
value: T,
|
value: T,
|
||||||
next: *const Node<T>,
|
next: Option<NonNull<Node<T>>>,
|
||||||
prev: *const Node<T>,
|
prev: Option<NonNull<Node<T>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> LinkedList<T> {
|
||||||
|
pub fn new() -> Self<T> {
|
||||||
|
Self {
|
||||||
|
start: None,
|
||||||
|
end: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> IntoIterator for LinkedList<T> {
|
||||||
|
type Item = T;
|
||||||
|
type IntoIter = IntoIter<T>;
|
||||||
|
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct IntoIter<T> {
|
||||||
|
item: Option<NonNull<Node<T>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> IntoIter<T> {
|
||||||
|
fn new(list: LinkedList<T>) -> Self {
|
||||||
|
Self { item: list.start }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Iterator for IntoIter<T> {
|
||||||
|
type Item = T;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
let next = self.item.take();
|
||||||
|
let ptr = match next {
|
||||||
|
None => return None,
|
||||||
|
Some(mut ptr) => unsafe { ptr.as_mut() },
|
||||||
|
};
|
||||||
|
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue