The LinkedList structure is a doubly-linked list as shown in
ProgramΒ 1.
It allows appending or removal, in constant time (i.e.: πͺ(1)), of
elements at each end.
Searching for an element is in πͺ(n), since it requires to iterate over
the list.
ββββββ ββββββ ββββββ ββββββ
push_front() ββ> β β ββ> β β ββ> β β ββ> β β <ββ push_back()
O(1) β 10 β β 5 β β 47 β β 13 β O(1)
pop_front() <ββ β β <ββ β β <ββ β β <ββ β β ββ> pop_back()
ββββββ ββββββ ββββββ ββββββProgramΒ 1:A doubly-linked list
Main methods of the LinkedList structure can be seen in
TableΒ 1.
TableΒ 1:Some methods of LinkedList
Method | Description |
|---|---|
| Moves all elements from another |
| Provides a reference to the back element. |
| Removes all elements. |
| Returns |
| Provides a reference to the front element. |
| Returns |
| Returns a front-to-back iterator. |
| Returns the number of elements. |
| Creates a new list. |
| Removes the back element and returns it. |
| Removes the front element and returns it. |
| Appends an element to the back. |
| Appends an element to the front. |
| Splits the list into two at the given index. The current list is cut at the splitting point, and another list containing the remaining values is returned. |
27.3.1Creating a new instanceΒΆ
To create a new linked list LinkedList<V>, an easy way, like for the
Vec type, is to let the compiler infer the type. Inserting
at least one value will give the compiler the information it needs:
use std::collections::LinkedList;
let mut names = LinkedList::new();
names.push_back(String::from("Paul"));
names.push_back(String::from("John"));Couldn't automatically determine type of variable `names`.
Please give it an explicit type.A LinkedList may also be created from a vector or an array:
let mut other_names = LinkedList::from([
"George".to_string(),
"Ringo".to_string(),
]);
other_names27.3.2ConcatenatingΒΆ
To concatenate two lists we used the append() method that adds
elements from a list to the current one:
names.append(&mut other_names);
names27.3.3Pushing & removingΒΆ
We can push back or front an element in the list:
names.push_front(String::from("Paul"));
namesAnd also remove elements using the pop_*() methods:
names.pop_back()27.3.4SearchingΒΆ
Searching inside a list must be made using an iterator.
The position() method of the Iterator trait searches for a value
and returns the found index:
names.iter().position(|x| x == "George")The find() method of the Iterator trait searches for a value and
returns it:
names.iter().find(|&x| x == "John")The find() method takes a reference, and iter() iterates over
references, thus we get double references for elements.
One way is to explicitly write the double reference with the variable
declaration inside the closure. This way we can use the variable
directly:
let l = std::collections::LinkedList::from([10, 50, 8, 4]);
l.iter().find(|&&x| x == 8)Another solution is to write only one reference and use the
dereference operator *:
l.iter().find(|&x| *x == 8)27.3.5SplittingΒΆ
The split_off() method cuts a list into two parts at an index. The
current list is cut at the index, while the second part becomes a new
list that is returned by the method:
(names.split_off(2), names)