Simple LinkedList by Myself and Gains using Rust

#[derive(Debug)]
struct Node<'a>{
    next : &'a mut Node<'a>,
    data : i32,
}

struct LinkedList<'b>{
    head : Node<'b>,// not use mut ,because of "let mut var = ..." of "mut" including mutability to inner members.
    //rear : &'b mut Node<'b>,
}

// macro_rules! ListAdd {
//     ($list:expr,$node:expr) => {
//          let mut t = Node{next:LinkedList::null(),data:$node};
//         if $list.head.data == 0{
//            $list.head.next = &mut t;
//            $list.rear = &mut t;
//            $list.head.data += 1;
//            return;
//         }
//         $list.rear.next = &mut t;
//         $list.rear = &mut t;
//         $list.head.data += 1;
//     };
// }

impl<'d> Node<'d>{
    pub fn new(d:i32)->Node<'d>{
        Node{
            next:Node::null(),
            data:d,
        }
    }
    pub fn null()->&'d mut Node<'d>{
        unsafe{
            &mut *(0 as *mut Node)
        }
    }
}

impl<'c> LinkedList<'c>{
    pub fn new()-> LinkedList<'c>{
     
        LinkedList{
            
             head : Node{next:Node::null(),data:0},
             //rear : Node::null(),
             
        }
    
    }

    pub fn append_child(&mut self,node : &'c mut Node<'c>){
        //let mut t = Box::new(Node{next:LinkedList::null(),data:d});
        //let mut t = Node::<'c>{next:LinkedList::null(),data:d};

        self.head.data += 1;
        if self.head.data == 0 {
            self.head.next = node;
            return;
        }
        unsafe{
            node.next = &mut *(self.head.next as *mut Node);
        }
        self.head.next = node;
        

        // if self.head.data == 0{
        //     self.head.next = node;
        //     self.rear = node;
        //     self.head.data +=1;
        //     return;
        // }
        // self.head.data += 1;
        
        // self.rear.next = node;
        
        // self.rear = node;
        
    }
    pub fn len(&self)-> i32{
        self.head.data
    }
    
    pub fn to_array(&self)->Vec<i32>{
        
        let mut arr = Vec::new();

        //   error[E0507]: cannot move out of borrowed content
        //   --> LinkedList2.rs:88:23
        //    |
        // 88 |         let mut ptr = self.head.next;
        //    |                       ^^^^----------
        //    |                       |
        //    |                       cannot move out of borrowed content
        //    |                       help: consider using a reference instead: `&self.head.next`

        /*
         * 
         * Pointing to content, reference variable --raw point 
         * in essence-- cannot "move" into another variable
         * 
         * let mut ptr = self.head.next;//error
         * 
         */ 
        let mut ptr = &self.head.next;
        let len = self.len();
        for _ in 0..len{
            
            //error[E0308]: mismatched types
            //   --> LinkedList2.rs:93:18
            //    |
            // 93 |            ptr = *&ptr.next;
            //    |                  ^^^^^^^^^^ expected mutable reference, found struct `Node`
            //    |
            //    = note: expected type `&&mut Node<'c>`
            //               found type `&'c mut Node<'c>`

            // error: aborting due to previous error

            /*
             * && <---> ** | & <---> *
             * one "&" to one "*"
             * Conception of reference and dereference
             * You must understand!
             * 
             * If you write the following code:
             * 
             * arr.push(ptr.data);
             * 
             * this is also right!
             * 
             */

            arr.push((**ptr).data);
            ptr = &ptr.next;
           
        }
        arr
        
    }
}


fn main(){
    println!("{}", "LinkedList Writing!");
    
    let mut l = LinkedList::new();
    

    /*
     * 
     * How to use "unsafe"?
     * You must learn about how "function" works!
     * You can use two or more references(raw pointer) pointing to just one content if 
     * you are sure this is safe way.
     * 
     * This is aiming at transmiting address content to "next".It is necessary.
     * After done,there is only one reference variable pointing to the variable
     * you defined in high scope layer it can own a long lifetime.
     * 
     */ 
    unsafe{
        let mut node = Node::new(12);
        l.append_child(&mut *(&mut node as *mut Node));
        let mut node = Node::new(11);
        l.append_child(&mut *(&mut node as *mut Node));
        let mut node = Node::new(13);
        l.append_child(&mut *(&mut node as *mut Node));
        let mut node = Node::new(15);
        l.append_child(&mut *(&mut node as *mut Node));
        let mut node = Node::new(17);
        l.append_child(&mut *(&mut node as *mut Node));
        let mut node = Node::new(18);
        l.append_child(&mut *(&mut node as *mut Node));
        let mut node = Node::new(19);
        l.append_child(&mut *(&mut node as *mut Node));
        let mut node = Node::new(10);
        l.append_child(&mut *(&mut node as *mut Node));
    }
    
    println!("{:?}", l.to_array());
    println!("{:?}", l.to_array());
    println!("{:?}", l.to_array());


    // ListAdd!(l,123);
    // ListAdd!(l,22);
    // ListAdd!(l,55);
    // ListAdd!(l,11);
    // ListAdd!(l,888);

    // println!("{:p}", Node::null());

    let a = Node::new(100);
    let b = &a;
    let c = &b;
    println!("a node : {:?}", a.data);
    println!("&a address {:p}", &a);
    println!("&&a address {:p}", &&a);
    println!("&&&a address {:p}", &&&a);
    println!("b node : {:?}", b.data);
    println!("&b address {:p}", &b);
    println!("&&b address {:p}", &&b);
    println!("&&&b address {:p}", &&&b);
    println!("c node : {:?}", c.data);
    println!("&c address {:p}", &c);
    println!("&&c address {:p}", &&c);
    println!("&&&c address {:p}", &&&c);
    println!("== &*b=&a =====");
    println!("&*b address {:p} (&a == {:p})", &*b,&a);
    println!("== &*c=&b =====");
    println!("&*c address {:p} (&b == {:p})", &*c,&b);
    println!("== &**c=&a =====");
    println!("&**c address {:p} (&a == {:p})", &**c,&a);
    println!("================");
    let d = &a;
    let f = d;
    println!("&d address {:p}", &d);
    println!("&f address {:p}", &f);
    println!("&*d:{:p} ?= {:p}:&*f  (&a == {:p})", &*d,&*f,&a);
 

}
LinkedList Writing!
[10, 19, 18, 17, 15, 13, 11, 12]
[10, 19, 18, 17, 15, 13, 11, 12]
[10, 19, 18, 17, 15, 13, 11, 12]
a node : 100
&a address 0x7ffe20a244b0
&&a address 0x7ffe20a245b8
&&&a address 0x7ffe20a24610
b node : 100
&b address 0x7ffe20a244c0
&&b address 0x7ffe20a24708
&&&b address 0x7ffe20a24760
c node : 100
&c address 0x7ffe20a244c8
&&c address 0x7ffe20a24858
&&&c address 0x7ffe20a248b0
== &*b=&a =====
&*b address 0x7ffe20a244b0 (&a == 0x7ffe20a244b0)
== &*c=&b =====
&*c address 0x7ffe20a244c0 (&b == 0x7ffe20a244c0)
== &**c=&a =====
&**c address 0x7ffe20a244b0 (&a == 0x7ffe20a244b0)
================
&d address 0x7ffe20a24ad0
&f address 0x7ffe20a24ad8
&*d:0x7ffe20a244b0 ?= 0x7ffe20a244b0:&*f  (&a == 0x7ffe20a244b0)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值