Este é meu código de lista encadeada simples, ainda tenho que implementar as funções pop from end e pop from front, mas estou tendo dificuldade em escrever a função pop from end
#![allow(warnings)]
#[derive(Clone)]
struct List {
el: i32,
next: Box<Option<List>>
}
impl List {
fn new(val: i32) -> List {
return List {
el: val,
next: Box::new(None)
}
}
fn from(arr: &[i32]) -> List {
let mut head = List::new(arr[0]);
let mut current = &mut head;
for &val in &arr[1..] {
/* current.next = Box::new(Some(List::new(val)));
current = (*current.next).as_mut().unwrap(); */
current.append(val);
}
return head
}
fn append(&mut self, val: i32) {
let mut current = self;
while let Some(ref mut next_node) = *current.next {
current = next_node;
}
current.next = Box::new(Some(List::new(val)));
}
fn prepend(&mut self, val:i32) {
// std::mem::replace(dest, src):
// Moves src into the referenced dest, returning the previous dest value
let old_self = std::mem::replace(self, List::new(val));
self.next = Box::new(Some(old_self));
}
}
fn main() {
let mut list = List::new(42);
list.append(32);
list.append(2321);
list.append(2839);
list.prepend(69);
list.pop(); // 2839 // difficulty in implementing this
println!("{}", list);
let mut list = List::from(&[1, 2, 3]);
list.prepend(0);
println!("{}", list);
}
Devo usar Rc
e RefCell
implementar uma lista encadeada única? Sou iniciante, então, por favor, ajude. Obrigado!