Understanding Lists

C++ list Container

The list container in C++ is a doubly linked list, which allows efficient insertion and deletion of elements at both ends and in the middle. This container is part of the C++ Standard Library and provides various member functions to manipulate the elements.

Common list Functions

push_back

Description: Adds an element to the end of the list.

Syntax:

void push_back(const T& value);

Example:

#include <iostream> #include <list> using namespace std; int main() { list<int> myList; myList.push_back(10); myList.push_back(20); myList.push_back(30); for (int val : myList) { cout << val << " "; } cout << endl; return 0; }

push_front

Description: Adds an element to the beginning of the list.

Syntax:

void push_front(const T& value);

Example:


pop_back

Description: Removes the last element from the list.

Syntax:

Example:


pop_front

Description: Removes the first element from the list.

Syntax:

Example:


front

Description: Accesses the first element of the list.

Syntax:

Example:


back

Description: Accesses the last element of the list.

Syntax:

Example:


insert

Description: Inserts elements at a specified position in the list.

Syntax:

Example:


erase

Description: Removes elements from the list at a specified position or range.

Syntax:

Example:


clear

Description: Removes all elements from the list.

Syntax:

Example:


size

Description: Returns the number of elements in the list.

Syntax:

Example:

These are some of the common functions available in the C++ list container. Each function serves a specific purpose, making it easier to manipulate and manage the elements in a doubly linked list. Understanding these functions and their usage is essential for effective programming with the STL list.

2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark