Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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

append(&other)

Moves all elements from another LinkedList into this one.

back()

Provides a reference to the back element.

clear()

Removes all elements.

contains(v)

Returns true if this list contains the value v. π’ͺ(n) time.

front()

Provides a reference to the front element.

is_empty()

Returns true if this list contains no elements.

iter()

Returns a front-to-back iterator.

len()

Returns the number of elements.

new()

Creates a new list.

pop_back()

Removes the back element and returns it.

pop_front()

Removes the front element and returns it.

push_back(v)

Appends an element to the back.

push_front(v)

Appends an element to the front.

split_off(i)

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:

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:

27.3.2ConcatenatingΒΆ

To concatenate two lists we used the append() method that adds elements from a list to the current one:

27.3.3Pushing & removingΒΆ

We can push back or front an element in the list:

And also remove elements using the pop_*() methods:

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:

The find() method of the Iterator trait searches for a value and returns it:

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:

Another solution is to write only one reference and use the dereference operator *:

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: