Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

테스트케이스: 연결 리스트

A possible way to implement a linked list of u32 elements is via enums:

enum LinkedList {
    // Next: Tuple struct that wraps an element and a boxed link to the next node
    Next(u32, Box<LinkedList>),
    // End: A node that signifies the end of the linked list
    End,
}

// `use` brings the enum variants into scope, so they can be used without the `LinkedList::` prefix
use LinkedList::*;

// 열거형에 메서드를 붙일 수 있습니다
impl LinkedList {
    // 빈 연결 리스트를 생성합니다
    fn new() -> LinkedList {
        // `End` has type `LinkedList`
        End
    }

    // Take ownership of a linked list, and return a new one with a new element at its front
    fn prepend(self, elem: u32) -> LinkedList {
        // `Next` also has type `LinkedList`, since it is a variant of the `LinkedList` enum
        Next(elem, Box::new(self))
    }

    // Return the length of the linked list
    fn len(&self) -> u32 {
        // `len` depends on which enum variant we have, so we pattern-match on `self`
        // Since this method only borrows `self`, the `tail` binding is also a borrow
        // Note: In Rust 2018+, match infers the needed references automatically
        match self {
            // Count this node and recursively count the rest of the linked list
            // Note: this is not tail-recursive and could overflow for very long linked lists
            Next(_, tail) => 1 + tail.len(),
            // Base case: the empty linked list has length 0.
            End => 0,
        }
    }
}

// Implement `Display` for `LinkedList` so it can be printed to the console using `println!`
impl std::fmt::Display for LinkedList {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Next(head, tail) => write!(f, "({}, {})", head, tail),
            End => write!(f, "End"),
        }
    }
}

fn main() {
    // 빈 연결 리스트를 생성합니다
    let mut list = LinkedList::new();

    // 몇 가지 요소를 앞에 추가합니다
    list = list.prepend(1);
    list = list.prepend(2);
    list = list.prepend(3);

    // 리스트의 최종 상태를 보여줍니다
    println!("연결 리스트의 길이는 {}입니다", list.len());
    println!("{}", list);
}

참고:

Box와 메서드