Created
March 8, 2024 15:46
-
-
Save alpaylan/2aa250985fd3eaff66c64e34b14f038b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fmt::Debug; | |
use std::fmt::Display; | |
use std::fmt; | |
struct DoubleLinkedList<T> { | |
arr: Vec<T>, | |
valid: Vec<bool>, | |
} | |
struct Node<'a, T> { | |
value: T, | |
index: usize, | |
arr: &'a DoubleLinkedList<T>, | |
} | |
impl<'a, T: Display> Display for Node<'a, T> { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
write!(f, "{}", self.value) | |
} | |
} | |
impl Debug for Node<'_, i32> { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
write!(f, "Node: {}", self.value) | |
} | |
} | |
impl <T: Copy + Clone> DoubleLinkedList<T> { | |
fn new() -> Self { | |
DoubleLinkedList { | |
arr: Vec::new(), | |
valid: Vec::new(), | |
} | |
} | |
fn push(&mut self, value: T) { | |
self.arr.push(value); | |
self.valid.push(true); | |
} | |
fn pop(&mut self) -> Option<T> { | |
if self.arr.len() == 0 { | |
return None; | |
} | |
let value = self.arr.pop(); | |
self.valid.pop(); | |
value | |
} | |
fn real_index(&self, index: usize) -> usize { | |
self.valid.iter().take(index).filter(|x| **x).count() | |
} | |
fn get(&self, index: usize) -> Option<Node<T>> { | |
let mut real_index = 0; | |
for i in 0..self.arr.len() { | |
if self.valid[i] { | |
if real_index == index { | |
return Some(Node { | |
value: self.arr[i], | |
index: i, | |
arr: self, | |
}); | |
} | |
real_index += 1; | |
} | |
} | |
None | |
} | |
fn remove(&mut self, index: usize) -> Option<T> { | |
if index >= self.arr.len() { | |
return None; | |
} else if !self.valid[index] { | |
return None; | |
} | |
self.valid[index] = false; | |
Some(self.arr[index]) | |
} | |
} | |
impl <'a, T: Clone + Copy + Debug> Node<'a, T> { | |
fn next(&self) -> Option<Node<T>> { | |
let next_index = self.arr.real_index(self.index) + 1; | |
self.arr.get(next_index) | |
} | |
fn prev(&self) -> Option<Node<T>> { | |
let prev_index = self.arr.real_index(self.index) - 1; | |
self.arr.get(prev_index) | |
} | |
} | |
fn main() { | |
let mut list : DoubleLinkedList<i32> = DoubleLinkedList::new(); | |
list.push(1); | |
list.push(2); | |
list.push(3); | |
let node = list.get(1).unwrap(); | |
println!("Value: {}", node.value); | |
let next = node.next().unwrap(); | |
println!("Next: {}", next.value); | |
let prev = node.prev().unwrap(); | |
println!("Prev: {}", prev.value); | |
list.remove(1); | |
let node = list.get(1).unwrap(); | |
println!("Value: {}", node.value); | |
let next = node.next(); | |
println!("Next: {:?}", next); | |
let prev = node.prev().unwrap(); | |
println!("Prev: {}", prev.value); | |
list.push(4); | |
let node = list.get(2).unwrap(); | |
println!("Value: {}", node.value); | |
let next = node.next(); | |
println!("Next: {:?}", next); | |
let prev = node.prev().unwrap(); | |
println!("Prev: {}", prev.value); | |
let dprev = prev.prev().unwrap(); | |
println!("PrevPrev: {}", dprev.value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment