# Help Wanted Simplest Linked List Implementation

**URL:** <https://internals.rust-lang.org/t/help-wanted-simplest-linked-list-implementation/6224>\
**Category:** Uncategorized\
**Created:** [November 11, 2017, 2:44am UTC](https://internals.rust-lang.org/t/help-wanted-simplest-linked-list-implementation/6224 "2017-11-11T02:44:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Piping](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/piping/32/2693_2.png) [@Piping](https://internals.rust-lang.org/u/Piping)\
**Post date:** [November 11, 2017, 2:44am UTC](https://internals.rust-lang.org/t/help-wanted-simplest-linked-list-implementation/6224/1 "2017-11-11T02:44:25Z")

</div>

Hi, I want to implement a most common and simplest linked list, but I cannot figure out how to implement it especially the pop\_back() part. Note that definition for the linked list and node should not be changed ( except for changing the box type??).

Here is my code:

```rust
/**
 * Mutable Lists
 * - Classic Lists
 */
#[derive(Debug)]
struct ClassicList<T> {
    head: Option<Box<ClassicListNode<T>>>,
}
#[derive(Debug)]
struct ClassicListNode<T> {
    data: T,
    next: Option<Box<ClassicListNode<T>>>,
}

impl<T> ClassicList<T> {
    fn new() -> Self {
        ClassicList { head: None }
    }
    fn push_back(&mut self, data: T) {
        let new_tail = Some(Box::new(ClassicListNode {
            data: data,
            next: None,
        }));
        if self.head.is_none() {
            self.head = new_tail;
        } else {
            let mut cur_box_node = self.head.as_mut().unwrap(); // &mut Box<ClassicListNode<T>>
            loop {
                if cur_box_node.next.is_none() {
                    break;
                }
                let temp = cur_box_node;
                cur_box_node = temp.next.as_mut().unwrap();
            }
            cur_box_node.next = new_tail;
        }
    }
    fn pop_back(&mut self) -> Option<T> {
        if self.head.is_none() {
            return None;
        } else {
            //HELP!!!
        }
    }
    fn push_front(&mut self, data: T) {
        let new_front = Some(Box::new(ClassicListNode {
            data: data,
            next: std::mem::replace(&mut self.head, None), // next = self.head
        }));
        self.head = new_front;
    }
    fn pop_front(&mut self) -> Option<T> {
        self.head.take().map(|node| {
            //node is Box<T>
            //take on-the-stack value apart
            let pop = *node;
            self.head = pop.next;
            pop.data
        })
    }
    fn concat(suffix: ClassicList<T>) {
        unimplemented!()
    }
    fn equals(rhs: &ClassicList<T>) -> bool {
        unimplemented!()
    }
    fn is_empty(&self) -> bool {
        match self.head {
            Some(_) => true,
            None => false,
        }
    }
    fn clear(&mut self) {
        unimplemented!()
    }
    fn length(&self) -> usize {
        unimplemented!()
    }
}

impl<T> ClassicListNode<T> {
    fn new(new_data: T) -> Self {
        ClassicListNode {
            data: new_data,
            next: None,
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![djc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/djc/32/1592_2.png) [@djc](https://internals.rust-lang.org/u/djc)\
**Post date:** [November 11, 2017, 8:37am UTC](https://internals.rust-lang.org/t/help-wanted-simplest-linked-list-implementation/6224/2 "2017-11-11T08:37:40Z")

</div>

This forum is for development of Rust itself. You should ask this question on the [https://users.rust-lang.org/](https://users.rust-lang.org/) forum.

---

<div class="post-metadata">

**Author:** ![Michael-F-Bryan](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/michael-f-bryan/32/2815_2.png) [@Michael-F-Bryan](https://internals.rust-lang.org/u/Michael-F-Bryan)\
**Post date:** [November 12, 2017, 2:51pm UTC](https://internals.rust-lang.org/t/help-wanted-simplest-linked-list-implementation/6224/3 "2017-11-12T14:51:56Z")

</div>

@piping, you may want to check out [Learn Rust by writing Entirely Too Many Linked Lists](https://github.com/rust-unofficial/too-many-lists).

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:29am UTC](https://internals.rust-lang.org/t/help-wanted-simplest-linked-list-implementation/6224/4 "2019-03-25T08:29:14Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
