Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. use std::boxed::Box;
  2.  
  3. pub struct List<T> {
  4. pub head: Option<Box<Node<T>>> ,
  5. pub size: u64
  6. }
  7.  
  8. impl <T> List<T> {
  9. pub fn new()-> List<T> {
  10. return List {
  11. head: Option::None,
  12. size: 0
  13. };
  14. }
  15.  
  16. pub fn push_back(&mut self, data: T) {
  17. match &self.head {
  18. Some(head) => {
  19. let mut node = Some((*head).clone());
  20. while (*(node).unwrap()).next.is_some() {
  21. node = (*node.unwrap()).next;
  22. }
  23. node.unwrap().next = Some(Box::new(Node::new(data)));
  24. },
  25. None => {
  26. self.head = Some(Box::new(Node::new(data)));
  27. }
  28. }
  29. }
  30. }
  31.  
  32. #[derive(Clone)]
  33. pub struct Node<T> {
  34. pub next: Option<Box<Node<T>>>,
  35. pub value: T
  36. }
  37.  
  38. impl<T> Node<T> {
  39. pub fn new(v: T)-> Node<T>{
  40. Node {
  41. next: Option::None,
  42. value: v
  43. }
  44. }
  45. }
  46.  
  47. fn main() {
  48. let mut l = List::<&str>::new();
  49. l.push_back("hello");
  50. l.push_back("world");
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement