Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::boxed::Box;
- pub struct List<T> {
- pub head: Option<Box<Node<T>>> ,
- pub size: u64
- }
- impl <T> List<T> {
- pub fn new()-> List<T> {
- return List {
- head: Option::None,
- size: 0
- };
- }
- pub fn push_back(&mut self, data: T) {
- match &self.head {
- Some(head) => {
- let mut node = Some((*head).clone());
- while (*(node).unwrap()).next.is_some() {
- node = (*node.unwrap()).next;
- }
- node.unwrap().next = Some(Box::new(Node::new(data)));
- },
- None => {
- self.head = Some(Box::new(Node::new(data)));
- }
- }
- }
- }
- #[derive(Clone)]
- pub struct Node<T> {
- pub next: Option<Box<Node<T>>>,
- pub value: T
- }
- impl<T> Node<T> {
- pub fn new(v: T)-> Node<T>{
- Node {
- next: Option::None,
- value: v
- }
- }
- }
- fn main() {
- let mut l = List::<&str>::new();
- l.push_back("hello");
- l.push_back("world");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement